View source
<?php
namespace Drupal\component_blocks\Plugin\Block;
use Drupal\Component\Plugin\Factory\DefaultFactory;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Entity\EntityDisplayBase;
use Drupal\Core\Entity\EntityMalformedException;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FormatterPluginManager;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\Context\ContextHandlerInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Render\Element;
use Drupal\Core\Render\Markup;
use Drupal\Core\Utility\Token;
use Drupal\layout_builder\Context\LayoutBuilderContextTrait;
use Drupal\ui_patterns\Definition\PatternDefinitionField;
use Drupal\ui_patterns\Definition\PatternDefinitionVariant;
use Drupal\ui_patterns\UiPatterns;
use Drupal\ui_patterns\UiPatternsManager;
use Drupal\ui_patterns_settings\Form\SettingsFormBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ComponentBlock extends BlockBase implements ContainerFactoryPluginInterface {
use LayoutBuilderContextTrait;
const FIXED = '__fixed';
private $uiPatternsManager;
protected $entityTypeManager;
private $contextHandler;
private $formatterPluginManager;
private $token;
protected $moduleHandler;
public function __construct(array $configuration, $plugin_id, array $plugin_definition, UiPatternsManager $uiPatternsManager, EntityTypeManagerInterface $entityTypeManager, ContextHandlerInterface $contextHandler, FormatterPluginManager $formatterPluginManager, Token $token, ModuleHandlerInterface $module_handler) {
$this->uiPatternsManager = $uiPatternsManager;
$this->entityTypeManager = $entityTypeManager;
$this->contextHandler = $contextHandler;
$this->formatterPluginManager = $formatterPluginManager;
$this->token = $token;
$this->moduleHandler = $module_handler;
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('plugin.manager.ui_patterns'), $container
->get('entity_type.manager'), $container
->get('context.handler'), $container
->get('plugin.manager.field.formatter'), $container
->get('token'), $container
->get('module_handler'));
}
public function setConfiguration(array $configuration) {
$defaultConfiguration = $this
->defaultConfiguration();
$plugin = $this
->uiPatternsManager()
->getDefinition($this->pluginDefinition['ui_pattern_id']);
foreach ($plugin['fields'] as $item) {
if (!($item['ui'] ?? TRUE)) {
unset($configuration['variables'][$item
->getName()]['value']);
}
}
$this->configuration = NestedArray::mergeDeep($this
->baseConfigurationDefaults(), $defaultConfiguration, $configuration);
}
public function defaultConfiguration() {
$plugin = $this
->uiPatternsManager()
->getDefinition($this->pluginDefinition['ui_pattern_id']);
$defaults = array_map(function (PatternDefinitionField $item) {
return [
'source' => self::FIXED,
'value' => $item['default'] ?? '',
];
}, $plugin['fields']);
return [
'label_display' => FALSE,
'variant' => NULL,
'variables' => $defaults,
'settings' => [],
];
}
public function build() {
$definition = $this
->uiPatternsManager()
->getDefinition($this->pluginDefinition['ui_pattern_id']);
$context = [];
$entity = $this
->getContextValue('entity');
$view_builder = $this->entityTypeManager
->getViewBuilder($entity
->getEntityTypeId());
$metadata = new BubbleableMetadata();
$metadata
->addCacheableDependency($entity);
foreach ($this
->getConfiguration()['variables'] as $context_id => $details) {
if ($details['source'] === self::FIXED) {
if (!is_scalar($details['value'])) {
$context[$context_id] = $details['value'];
continue;
}
try {
$value = $this->token
->replace($details['value'], [
$entity
->getEntityTypeId() => $entity,
], [], $metadata);
if ($value !== $details['value']) {
$value = Markup::create($value);
}
} catch (EntityMalformedException $e) {
$value = '';
}
$context[$context_id] = $value;
continue;
}
try {
$formatter_output = $view_builder
->viewField($entity
->get($details['source']), array_intersect_key($details, [
'type' => TRUE,
'settings' => TRUE,
]) + [
'label' => 'hidden',
]);
if (Element::isEmpty($formatter_output)) {
$metadata
->merge(CacheableMetadata::createFromRenderArray($formatter_output));
continue;
}
$context[$context_id] = [
'#theme' => 'field__component_block',
] + $formatter_output;
} catch (EntityMalformedException $e) {
$context[$context_id] = '';
}
}
$build = [
'#type' => 'pattern',
'#id' => $this->pluginDefinition['ui_pattern_id'],
'#fields' => $context,
'#context' => [
'type' => 'entity',
'entity' => $entity,
],
];
if (isset($this
->getConfiguration()['variant'])) {
$build['#variant'] = $this
->getConfiguration()['variant'];
}
if (isset($this
->getConfiguration()['settings'])) {
$build['#settings'] = $this
->getConfiguration()['settings'];
}
if (!empty($definition['libraries'])) {
$metadata
->addAttachments([
'library' => $definition
->getLibrariesNames(),
]);
}
$metadata
->applyTo($build);
return $build;
}
public function blockForm($form, FormStateInterface $form_state) {
$plugin = $this
->uiPatternsManager()
->getDefinition($this->pluginDefinition['ui_pattern_id']);
$form = parent::blockForm($form, $form_state);
if (!empty($plugin['variants'])) {
$form['variant'] = [
'#type' => 'select',
'#title' => $this
->t('Variant'),
'#default_value' => $this
->getConfiguration()['variant'] ?? NULL,
'#options' => array_map(function (PatternDefinitionVariant $item) {
return $item
->getLabel();
}, $plugin['variants']),
];
}
$form['variables'] = [
'#type' => 'details',
'#title' => $this
->t('Context variables'),
'#open' => TRUE,
'#tree' => TRUE,
];
$contexts = $this
->contextHandler()
->getMatchingContexts($form_state
->getTemporaryValue('gathered_contexts') ?: [], $this
->getContextDefinition('entity'));
$context = reset($contexts);
$sample_entity = $context
->getContextData()
->getValue();
$fields = array_map(function (FieldDefinitionInterface $field) {
return $field
->getLabel();
}, $sample_entity
->getFieldDefinitions());
$fields[self::FIXED] = $this
->t('Fixed input');
foreach ($plugin['fields'] as $id => $details) {
if (!($details['ui'] ?? TRUE)) {
$form['variables'][$id] = [
'source' => [
'#type' => 'value',
'#value' => self::FIXED,
],
'value' => [
'#type' => 'value',
'#value' => $details['default'],
],
];
continue;
}
$form['variables'][$id] = [
'#type' => 'container',
'#process' => [
[
$this,
'formatterSettingsProcessCallback',
],
],
'#prefix' => '<div id="component-settings-' . $id . '">',
'#suffix' => '</div>',
'label' => [
'#type' => 'item',
'#markup' => $details['label'],
],
'source' => [
'#type' => 'select',
'#options' => $fields,
'#default_value' => self::FIXED,
'#title' => $this
->t('Source'),
'#ajax' => [
'callback' => [
get_class($this),
'updateElementValue',
],
'wrapper' => 'component-settings-' . $id,
],
],
];
}
if ($this->moduleHandler
->moduleExists('ui_patterns_settings')) {
$configuration['pattern']['settings'] = $this
->getConfiguration()['settings'];
$definition = UiPatterns::getPatternDefinition($this->pluginDefinition['ui_pattern_id']);
SettingsFormBuilder::layoutForm($form, $definition, $configuration);
$form['settings'] = array_merge($form['settings'], [
'#type' => 'details',
'#title' => $this
->t('Pattern settings'),
'#open' => TRUE,
'#tree' => TRUE,
]);
}
return $form;
}
public function formatterSettingsProcessCallback(array &$element, FormStateInterface $form_state, array &$complete_form) {
if ($configuration = $this
->getCurrentConfiguration($element['#parents'], $form_state)) {
if ($configuration['source'] === self::FIXED) {
$element['value'] = [
'#type' => 'textfield',
'#default_value' => $configuration['value'] ?? '',
'#title' => $this
->t('Fixed value'),
];
return $element;
}
$contexts = $this
->contextHandler()
->getMatchingContexts($form_state
->getTemporaryValue('gathered_contexts') ?: [], $this
->getContextDefinition('entity'));
if (!$contexts) {
$contexts = $this
->contextHandler()
->getMatchingContexts($this
->getAvailableContexts($form_state
->getBuildInfo()['args'][0]), $this
->getContextDefinition('entity'));
}
$context = reset($contexts);
$sample_entity = $context
->getContextData()
->getValue();
$field_definition = $sample_entity
->getFieldDefinition($configuration['source']);
$formatter_configuration = array_intersect_key($configuration, [
'type' => TRUE,
'settings' => TRUE,
]) + [
'label' => 'hidden',
];
$options = $this
->getApplicablePluginOptions($field_definition);
$keys = array_keys($options);
$formatter_configuration += [
'type' => reset($keys),
'settings' => $this
->formatterPluginManager()
->getDefaultSettings(reset($keys)),
];
$formatter = $this
->formatterPluginManager()
->getInstance([
'configuration' => $formatter_configuration,
'field_definition' => $field_definition,
'view_mode' => EntityDisplayBase::CUSTOM_MODE,
'prepare' => TRUE,
]);
$element['source']['#default_value'] = $configuration['source'];
$element['type'] = [
'#type' => 'select',
'#options' => $options,
'#default_value' => $formatter_configuration['type'],
'#required' => TRUE,
'#title' => $this
->t('Formatter'),
'#ajax' => [
'callback' => [
static::class,
'updateElementValue',
],
'wrapper' => 'component-settings-' . end($element['#parents']),
],
];
$element['settings'] = $formatter
->settingsForm($complete_form, $form_state);
$element['settings']['#parents'] = array_merge($element['#parents'], [
'settings',
]);
}
return $element;
}
protected function getCurrentConfiguration(array $parents, FormStateInterface $form_state) : ?array {
$configuration = NestedArray::getValue($form_state
->getValues(), $parents);
$variable = end($parents);
if (!$configuration) {
$configuration = NestedArray::getValue($form_state
->getUserInput(), $parents);
if (!$configuration) {
$settings = $this
->getConfiguration()['variables'][$variable];
return $settings;
}
}
return $configuration;
}
public static function updateElementValue(array $form, FormStateInterface $form_state) {
$array_parents = $form_state
->getTriggeringElement()['#array_parents'];
array_pop($array_parents);
return NestedArray::getValue($form, $array_parents);
}
public function blockSubmit($form, FormStateInterface $form_state) {
$this->configuration['variant'] = $form_state
->getValue('variant');
$this->configuration['variables'] = $form_state
->getValue('variables');
$this->configuration['settings'] = $form_state
->getValue('settings');
}
protected function getApplicablePluginOptions(FieldDefinitionInterface $field_definition) {
$options = $this
->formatterPluginManager()
->getOptions($field_definition
->getType());
$applicable_options = [];
foreach ($options as $option => $label) {
$plugin_class = DefaultFactory::getPluginClass($option, $this
->formatterPluginManager()
->getDefinition($option));
if ($plugin_class::isApplicable($field_definition)) {
$applicable_options[$option] = $label;
}
}
return $applicable_options;
}
protected function contextHandler() {
return $this->contextHandler ?: \Drupal::service('context.handler');
}
protected function formatterPluginManager() : FormatterPluginManager {
if (!$this->formatterPluginManager) {
$this->formatterPluginManager = \Drupal::service('plugin.manager.field.formatter');
}
return $this->formatterPluginManager;
}
protected function uiPatternsManager() : UiPatternsManager {
if (!$this->uiPatternsManager) {
$this->uiPatternsManager = \Drupal::service('plugin.manager.ui_patterns');
}
return $this->uiPatternsManager;
}
}