react_block-8.x-1.x-dev/src/Plugin/Field/FieldWidget/ReactBlockOptions.php
src/Plugin/Field/FieldWidget/ReactBlockOptions.php
<?php
namespace Drupal\react_block\Plugin\Field\FieldWidget;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldFilteredMarkup;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
/**
* A widget to input video URLs.
*
* @FieldWidget(
* id = "react_block_options",
* label = @Translation("React Blocks"),
* field_types = {
* "react_block"
* },
* )
*/
class ReactBlockOptions extends WidgetBase {
protected $column;
/**
* {@inheritdoc}
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings);
$property_names = $this->fieldDefinition->getFieldStorageDefinition()->getPropertyNames();
$this->column = $property_names[0];
}
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element['value']['#key_column'] = $this->column;
$element['value'] = $element + [
'#type' => 'select',
'#options' => $this->getOptions($items->getEntity()),
'#default_value' => isset($items[$delta]->value) ? $items[$delta]->value : NULL,
];
return $element;
}
/**
* Returns the array of options for the widget.
*
* @param \Drupal\Core\Entity\FieldableEntityInterface $entity
* The entity for which to return options.
*
* @return array
* The array of options for the widget.
*/
protected function getOptions(FieldableEntityInterface $entity) {
if (!isset($this->options)) {
// Limit the settable options for the current user account.
$options = $this->fieldDefinition
->getFieldStorageDefinition()
->getOptionsProvider($this->column, $entity)
->getSettableOptions(\Drupal::currentUser());
// Add an empty option if the widget needs one.
if ($empty_label = $this->getEmptyLabel()) {
$options = ['_none' => $empty_label] + $options;
}
$module_handler = \Drupal::moduleHandler();
$context = [
'fieldDefinition' => $this->fieldDefinition,
'entity' => $entity,
];
$module_handler->alter('options_list', $options, $context);
array_walk_recursive($options, [$this, 'sanitizeLabel']);
$this->options = $options;
}
return $this->options;
}
/**
* Sanitizes a string label to display as an option.
*
* @param string $label
* The label to sanitize.
*/
protected function sanitizeLabel(&$label) {
// Allow a limited set of HTML tags.
$label = FieldFilteredMarkup::create($label);
}
/**
* {@inheritdoc}
*/
protected function getEmptyLabel() {
return t('- None -');
}
}
