ui_suite_dsfr_ft-1.0.0-rc3/src/Plugin/UiPatterns/Source/MenuLinksAsLinks.php
src/Plugin/UiPatterns/Source/MenuLinksAsLinks.php
<?php
declare(strict_types=1);
namespace Drupal\ui_suite_dsfr_ft\Plugin\UiPatterns\Source;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Template\Attribute;
use Drupal\ui_patterns\Attribute\Source;
use Drupal\ui_patterns\AttributesTrait;
use Drupal\ui_patterns\EnumTrait;
use Drupal\ui_patterns\Plugin\UiPatterns\Source\AttributesWidget;
use Drupal\ui_patterns\Plugin\UiPatterns\Source\MenuSource;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Plugin implementation of the source.
*/
#[Source(
id: 'ui_suite_dsfr_fr_menu_links_as_links',
label: new TranslatableMarkup('Menu Links rendered as links'),
description: new TranslatableMarkup('Render level 1 of a drupal menu into links'),
prop_types: ['slot']
)]
class MenuLinksAsLinks extends MenuSource {
use EnumTrait;
use AttributesTrait;
/**
* Component plugin manager.
*
* @var \Drupal\Core\Theme\ComponentPluginManager
*/
protected $componentManager;
/**
* {@inheritdoc}
*/
public static function create(
ContainerInterface $container,
array $configuration,
$plugin_id,
$plugin_definition,
) {
$plugin = parent::create(
$container,
$configuration,
$plugin_id,
$plugin_definition
);
$plugin->componentManager = $container->get('plugin.manager.sdc');
return $plugin;
}
/**
* {@inheritdoc}
*/
public function defaultSettings(): array {
return array_merge(parent::defaultSettings(), ['depth' => 1, "link_variant" => "", 'link_attributes' => '']);
}
/**
* {@inheritdoc}
*/
public function getPropValue(): mixed {
$items = parent::getPropValue() ?? [];
$variant_id = $this->getSetting('link_variant') ?? '';
$link_attributes_base = static::convertValueToAttributesMapping($this->getSetting('link_attributes') ?? '');
$link_attributes_base = new Attribute($link_attributes_base);
$build = [];
foreach ($items as $item) {
$link_attributes = $item['link_attributes'] ?? [];
$link_attributes_object = new Attribute($link_attributes);
$link_attributes_object = $link_attributes_object->merge($link_attributes_base);
$build[] = [
"#type" => "component",
"#component" => "ui_suite_dsfr:link",
"#slots" => [
"label" => $item["title"],
],
"#props" => [
'variant' => $variant_id,
'url' => $item["url"],
'attributes' => $link_attributes_object,
],
];
}
return $build;
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state): array {
$form = parent::settingsForm($form, $form_state);
$form['level']['#default_value'] = 1;
$form['level']['#access'] = FALSE;
$form['depth']['#default_value'] = 1;
$form['depth']['#access'] = FALSE;
$link_component = $this->componentManager->find('ui_suite_dsfr:link');
$variant_definition = $link_component->metadata->schema['properties']["variant"];
$options = self::getEnumOptions($variant_definition);
$form["link_variant"] = [
'#type' => 'select',
'#title' => new TranslatableMarkup('Link variant'),
'#options' => $options,
'#default_value' => $this->getSetting('link_variant') ?? '',
];
$value = $this->getSetting('link_attributes') ?? '';
$form['link_attributes'] = [
'#type' => 'textfield',
'#default_value' => $value,
];
if (static::isValueForAttributes($value)) {
$form['link_attributes']['#pattern_unicode'] = static::buildAttributesRegexPattern();
$form['link_attributes']['#pattern_error'] = $this->t('Format is not HTML attributes with double-quoted values.');
}
else {
$form['link_attributes']['#pattern_unicode'] = static::buildClassRegexPattern();
$form['link_attributes']['#pattern_error'] = $this->t('Not a valid space-separated list of HTML classes.');
}
// To allow form errors to be displayed correctly.
$form['link_attributes']['#title'] = '';
$form['link_attributes']['#placeholder'] = 'class="hidden" title="Lorem ipsum"';
$form['link_attributes']['#description'] = $this->t("HTML attributes with double-quoted values or a space-separated list of HTML classes.");
$form['link_attributes']['#element_validate'][] = [AttributesWidget::class, 'validateUnicodePattern'];
return $form;
}
}
