Skip to content

Commit

Permalink
ENH: Insert / edit link button label.
Browse files Browse the repository at this point in the history
  • Loading branch information
mfendeksilverstripe committed Jun 6, 2023
1 parent d425b33 commit 3041730
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
4 changes: 4 additions & 0 deletions _config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ SilverStripe\Forms\TreeDropdownField:
SilverStripe\CMS\Forms\AnchorSelectorField:
extensions:
- SilverStripe\LinkField\Extensions\AjaxField

SilverStripe\LinkField\Form\FormFactory:
extensions:
- SilverStripe\LinkField\Extensions\FormFactoryExtension
55 changes: 55 additions & 0 deletions src/Extensions/FormFactoryExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace SilverStripe\LinkField\Extensions;

use SilverStripe\Control\RequestHandler;
use SilverStripe\Core\Extension;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\FormAction;
use SilverStripe\LinkField\Form\FormFactory;
use SilverStripe\LinkField\Models\Link;

/**
* Enhance the insert / edit link button label to match the model data state
*
* @method FormFactory getOwner()
*/
class FormFactoryExtension extends Extension
{
/**
* Extension point in @see FormFactory::getFormActions()
*
* @param FieldList $actions
* @param RequestHandler $controller
* @param string $name
* @param array $context
* @return void
*/
public function updateFormActions(FieldList $actions, RequestHandler $controller, string $name, array $context): void
{
if (!array_key_exists('LinkType', $context)) {
// We couldn't find any link model
return;
}

/** @var Link $linkType */
$linkType = $context['LinkType'];

if (!$linkType->exists()) {
// This is a new link, so we don't need to to any further customisation
return;
}

/** @var FormAction $insertAction */
$insertAction = $actions->fieldByName('action_insert');

if (!$insertAction) {
// We couldn't find the insert action
return;
}

// Update the title of the action to reflect the link model data state
$insertActionTitle = _t('Admin.EDIT_LINK', 'Edit link');
$insertAction->setTitle($insertActionTitle);
}
}

0 comments on commit 3041730

Please sign in to comment.