Skip to content

PR - Slug Field #4605

@gbrits

Description

@gbrits

Pseudo pull request I guess - inside of the Slug field:

The showCustomizeButton method doesn't exist.

/**
 * Set whether or not the customize button is visible / used to enable slug editing.
 *
 * @param  boolean  $showCustomizeButton
 * @return $this
 */
public function showCustomizeButton($showCustomizeButton)
{
    $this->showCustomizeButton = $showCustomizeButton;
    $this->customizeButtonExplicitlySet = true;
    return $this;
}

Adding new public variable at the top:
public $customizeButtonExplicitlySet = false;

Then adjust one line inside of jsonSerialize():

/**
 * Prepare the element for JSON serialization.
 *
 * @return array<string, mixed>
 */
public function jsonSerialize(): array
{
    $request = app(NovaRequest::class);

    if ($request->isUpdateOrUpdateAttachedRequest()) {
        $this->readonly();
+        if($this->customizeButtonExplicitlySet === false) $this->showCustomizeButton = true;
    }

    return array_merge([
        'updating' => $request->isUpdateOrUpdateAttachedRequest(),
        'from' => $this->from instanceof Field ? $this->from->attribute : str_replace(' ', '_', Str::lower($this->from)),
        'separator' => $this->separator,
        'showCustomizeButton' => $this->showCustomizeButton,
    ], parent::jsonSerialize());
}

This will allow users to control whether or not the Customize button appears next to a slug, even in editing, based on the user's reasoning/criteria. In my instance I needed to admin-protect certain slugs.

$admin_protected_slugs = [
    'about',
    'contact'
];

...

Slug::make('Slug')->from('Title')->separator('-')
->showCustomizeButton(!(boolean)in_array($this->resource->slug, $admin_protected_slugs))
->withMeta($page_meta)->hideFromIndex()->sortable(),

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions