Skip to content
Snippets Groups Projects
Verified Commit 00c2772c authored by Julian Pustkuchen's avatar Julian Pustkuchen Committed by Lee Rowlands
Browse files

Issue #3267374 by Grevil, Anybody, larowlan, maursilveira: Add option to...

Issue #3267374 by Grevil, Anybody, larowlan, maursilveira: Add option to choose widget open by default or not
parent 48b0be39
No related branches found
No related tags found
No related merge requests found
......@@ -13,3 +13,6 @@ field.widget.settings.link_attributes:
sequence:
type: boolean
label: 'Enabled'
widget_default_open:
type: string
label: 'Widget expand behavior'
......@@ -2,5 +2,6 @@ name: Link attributes
type: module
description: Provides a widget to allow settings of link attributes for link fields.
core_version_requirement: ^9 || ^10
php: 8.0
dependencies:
- drupal:link
......@@ -23,6 +23,10 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
*/
class LinkWithAttributesWidget extends LinkWidget implements ContainerFactoryPluginInterface {
public const WIDGET_OPEN_EXPAND_IF_VALUES_SET = 'expandIfValuesSet';
public const WIDGET_OPEN_COLLAPSED = 'collapsed';
public const WIDGET_OPEN_EXPANDED = 'expanded';
/**
* The link attributes manager.
*
......@@ -80,6 +84,7 @@ class LinkWithAttributesWidget extends LinkWidget implements ContainerFactoryPlu
'class' => TRUE,
'accesskey' => FALSE,
],
'widget_default_open' => self::WIDGET_OPEN_EXPAND_IF_VALUES_SET,
] + parent::defaultSettings();
}
......@@ -98,11 +103,22 @@ class LinkWithAttributesWidget extends LinkWidget implements ContainerFactoryPlu
if (empty(array_filter($this->getSetting('enabled_attributes')))) {
return $element;
}
$widgetDefaultOpenSetting = $this->getSetting('widget_default_open');
$open = NULL;
match ($widgetDefaultOpenSetting) {
self::WIDGET_OPEN_EXPAND_IF_VALUES_SET => $open = count($attributes),
self::WIDGET_OPEN_EXPANDED => $open = TRUE,
self::WIDGET_OPEN_COLLAPSED => $open = FALSE,
default => $open = count($attributes),
};
$element['options']['attributes'] = [
'#type' => 'details',
'#title' => $this->t('Attributes'),
'#tree' => TRUE,
'#open' => count($attributes),
'#open' => $open,
];
$plugin_definitions = $this->linkAttributesManager->getDefinitions();
foreach (array_keys(array_filter($this->getSetting('enabled_attributes'))) as $attribute) {
......@@ -145,6 +161,17 @@ class LinkWithAttributesWidget extends LinkWidget implements ContainerFactoryPlu
'#default_value' => array_combine($selected, $selected),
'#description' => $this->t('Select the attributes to allow the user to edit.'),
];
$element['widget_default_open'] = [
'#type' => 'select',
'#title' => $this->t('Widget default open behavior'),
'#options' => [
self::WIDGET_OPEN_EXPAND_IF_VALUES_SET => $this->t('Expand if values set (Default)'),
self::WIDGET_OPEN_EXPANDED => $this->t('Expand'),
self::WIDGET_OPEN_COLLAPSED => $this->t('Collapse'),
],
'#default_value' => $this->getSetting('default_open') ?? self::WIDGET_OPEN_EXPAND_IF_VALUES_SET,
'#description' => $this->t('Set the widget default open behavior.'),
];
return $element;
}
......@@ -178,6 +205,13 @@ class LinkWithAttributesWidget extends LinkWidget implements ContainerFactoryPlu
if ($enabled_attributes) {
$summary[] = $this->t('With attributes: @attributes', ['@attributes' => implode(', ', array_keys($enabled_attributes))]);
}
$widgetDefaultOpenSetting = $this->getSetting('widget_default_open');
match ($widgetDefaultOpenSetting) {
self::WIDGET_OPEN_EXPAND_IF_VALUES_SET => $summary[] = $this->t('Widget open if values set'),
self::WIDGET_OPEN_EXPANDED => $summary[] = $this->t('Widget open by default.'),
self::WIDGET_OPEN_COLLAPSED => $summary[] = $this->t('Widget closed by default.'),
};
return $summary;
}
......
......@@ -2,6 +2,7 @@
namespace Drupal\Tests\link_attributes\Functional;
use Drupal\link_attributes\Plugin\Field\FieldWidget\LinkWithAttributesWidget;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\field_ui\Traits\FieldUiTestTrait;
......@@ -84,6 +85,7 @@ class LinkAttributesFieldTest extends BrowserTestBase {
'class' => TRUE,
'target' => TRUE,
],
'widget_default_open' => LinkWithAttributesWidget::WIDGET_OPEN_EXPANDED,
],
])
->save();
......@@ -189,4 +191,102 @@ class LinkAttributesFieldTest extends BrowserTestBase {
$web_assert->linkExists('Link One');
}
/**
* Tests the widget's "widget_default_open" option.
*/
public function testWidgetDetailsBehavior() {
$session = $this->assertSession();
// Add a content type.
$type = $this->drupalCreateContentType();
$type_path = 'admin/structure/types/manage/' . $type->id();
$add_path = 'node/add/' . $type->id();
// Add a link field to the newly-created type.
$label = $this->randomMachineName();
$field_name = mb_strtolower($label);
$storage_settings = ['cardinality' => 'number', 'cardinality_number' => 2];
$this->fieldUIAddNewField($type_path, $field_name, $label, 'link', $storage_settings);
// Manually clear cache on the tester side.
\Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
// Change the link widget and set the "widget_default_open" option
// to "expanded":
$defaultFormDisplay = \Drupal::entityTypeManager()
->getStorage('entity_form_display')
->load('node.' . $type->id() . '.default');
$defaultFormDisplay->setComponent('field_' . $field_name, [
'type' => 'link_attributes',
'settings' => [
'widget_default_open' => LinkWithAttributesWidget::WIDGET_OPEN_EXPANDED,
'enabled_attributes' => [
'rel' => TRUE,
'class' => TRUE,
'target' => TRUE,
],
],
])->save();
$this->drupalGet($add_path);
// See if the details are open:
$session->elementAttributeExists('css', '#edit-field-' . $field_name . '-0-options-attributes', 'open');
// Change the link widget and the "widget_default_open" option to
// "collapsed":
$defaultFormDisplay->setComponent('field_' . $field_name, [
'type' => 'link_attributes',
'settings' => [
'widget_default_open' => LinkWithAttributesWidget::WIDGET_OPEN_COLLAPSED,
'enabled_attributes' => [
'rel' => TRUE,
'class' => TRUE,
'target' => TRUE,
],
],
])
->save();
// See if the details are closed:
$this->drupalGet($add_path);
$session->elementAttributeNotExists('css', '#edit-field-' . $field_name . '-0-options-attributes', 'open');
// Change the link widget and the "widget_default_open" option to
// "expandIfValuesSet" and have no attributes enabled:
$defaultFormDisplay->setComponent('field_' . $field_name, [
'type' => 'link_attributes',
'settings' => [
'widget_default_open' => LinkWithAttributesWidget::WIDGET_OPEN_EXPAND_IF_VALUES_SET,
'enabled_attributes' => [
'rel' => TRUE,
'class' => TRUE,
'target' => TRUE,
],
],
])
->save();
\Drupal::state()->set('link_attributes_test_alterinfo.hook_link_attributes_plugin_alter', FALSE);
\Drupal::service('plugin.manager.link_attributes')->clearCachedDefinitions();
// See if the details are closed, as no attributes are set:
$this->drupalGet($add_path);
$session->elementAttributeNotExists('css', '#edit-field-' . $field_name . '-0-options-attributes', 'open');
// Create a node and set its attributes:
$edit = [
'title[0][value]' => 'A multi field link test',
'field_' . $field_name . '[0][title]' => 'Link One',
'field_' . $field_name . '[0][uri]' => '<front>',
'field_' . $field_name . '[0][options][attributes][class]' => 'class-one class-two',
'field_' . $field_name . '[1][title]' => 'Link Two',
'field_' . $field_name . '[1][uri]' => '<front>',
'field_' . $field_name . '[1][options][attributes][class]' => 'class-three class-four',
];
$this->submitForm($edit, 'Save');
$node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
// See if the details are open now, as attributes are set:
$this->drupalGet('node/' . $node->id() . '/edit');
$session->elementAttributeExists('css', '#edit-field-' . $field_name . '-0-options-attributes', 'open');
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment