Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Insert / edit link button label. #65

Merged
merged 1 commit into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}