View source
<?php
namespace Drupal\social_content_block\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\social_content_block\ContentBlockManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ContentBlockPluginFieldWidget extends ContentBlockPluginWidgetBase {
const CONFIG_PREFIX = 'field.field.block_content.custom_content_list.';
protected $fieldConfigs;
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, ContentBlockManagerInterface $content_block_manager, array $field_configs) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings, $content_block_manager);
$this->fieldConfigs = $field_configs;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['third_party_settings'], $container
->get('plugin.manager.content_block'), $container
->get('config.factory')
->listAll(self::CONFIG_PREFIX));
}
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element = parent::formElement($items, $delta, $element, $form, $form_state);
$selected_plugin_id = $items
->getEntity()->field_plugin_id->value;
$selector = $this->contentBlockManager
->getSelector('field_plugin_id', 'value', $element['#field_parents']);
foreach ($this->contentBlockManager
->getDefinitions() as $plugin_id => $plugin_definition) {
$element[$plugin_id] = [
'#type' => 'select',
'#title' => $element['value']['#title'],
'#description' => $element['value']['#description'],
'#empty_value' => 'all',
'#empty_option' => t('All'),
'#weight' => -1,
'#states' => [
'visible' => [
$selector => [
'value' => $plugin_id,
],
],
],
];
if ($selected_plugin_id === $plugin_id) {
$element[$plugin_id]['#default_value'] = $element['value']['#default_value'];
}
foreach ($plugin_definition['fields'] as $field) {
if (isset($form[$field])) {
if (isset($form[$field]['widget']['target_id']['#title'])) {
$element[$plugin_id]['#options'][$field] = $form[$field]['widget']['target_id']['#title'];
}
elseif (isset($form[$field]['widget']['#title'])) {
$element[$plugin_id]['#options'][$field] = $form[$field]['widget']['#title'];
}
else {
$element[$plugin_id]['#options'][$field] = "-- Could not find widget title for '{$field}' in " . self::class . ' --';
}
$form[$field]['#states'] = [
'visible' => [
$selector => [
'value' => $plugin_id,
],
$this->contentBlockManager
->getSelector('field_plugin_field', $plugin_id) => [
[
'value' => 'all',
],
[
'value' => $field,
],
],
],
];
}
elseif (in_array(self::CONFIG_PREFIX . $field, $this->fieldConfigs)) {
$element[$plugin_id]['#options'][$field] = $field;
}
}
}
$element['#element_validate'][] = [
get_class($this),
'validateElement',
];
return $element;
}
public static function validateElement(array $element, FormStateInterface $form_state) {
$value = $form_state
->getValue([
'field_plugin_field',
0,
$form_state
->getValue([
'field_plugin_id',
0,
'value',
]),
]);
if ($value === 'all') {
$form_state
->setValueForElement($element, NULL);
}
else {
$form_state
->setValueForElement($element, [
'value' => $value,
]);
}
}
}