AttributesSettingType.php in UI Patterns Settings 8
Same filename and directory in other branches
File
src/Plugin/UiPatterns/SettingType/AttributesSettingType.phpView source
<?php
namespace Drupal\ui_patterns_settings\Plugin\UIPatterns\SettingType;
use Drupal\Component\Utility\Html;
use Drupal\Core\Template\Attribute;
use Drupal\ui_patterns_settings\Definition\PatternDefinitionSetting;
use Drupal\ui_patterns_settings\Plugin\PatternSettingTypeBase;
/**
* Attributes setting type.
*
* @UiPatternsSettingType(
* id = "attributes",
* label = @Translation("Attributes")
* )
*/
class AttributesSettingType extends PatternSettingTypeBase {
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, $value, PatternDefinitionSetting $def) {
$value = $this
->getValue($value);
$description = $def
->getDescription() != NULL ? $def
->getDescription() : $this
->t('E.g. role="navigation" class="class-1"');
$form[$def
->getName()] = [
'#type' => 'textfield',
'#title' => $def
->getLabel(),
'#description' => $description,
'#default_value' => $value,
'#required' => $def
->getRequired(),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function preprocess($value, array $context) {
$value = parent::preprocess($value, $context);
$parse_html = '<div ' . $value . '></div>';
$attributes = [];
foreach (Html::load($parse_html)
->getElementsByTagName('div') as $div) {
foreach ($div->attributes as $attr) {
$attributes[$attr->nodeName] = $attr->nodeValue;
}
}
return new Attribute($attributes);
}
}
Classes
Name | Description |
---|---|
AttributesSettingType | Attributes setting type. |