class EntityqueueDragtableWidget in Entityqueue 8
Plugin implementation of the 'entityqueue_dragtable' widget.
Plugin annotation
@FieldWidget(
id = "entityqueue_dragtable",
label = @Translation("Autocomplete (draggable table)"),
description = @Translation("An autocomplete text field with a draggable table."),
field_types = {
"entity_reference"
}
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\Core\Field\PluginSettingsBase implements DependentPluginInterface, PluginSettingsInterface
- class \Drupal\Core\Field\WidgetBase implements WidgetInterface, ContainerFactoryPluginInterface uses AllowedTagsXssTrait
- class \Drupal\Core\Field\Plugin\Field\FieldWidget\EntityReferenceAutocompleteWidget
- class \Drupal\entityqueue\Plugin\Field\FieldWidget\EntityqueueDragtableWidget
- class \Drupal\Core\Field\Plugin\Field\FieldWidget\EntityReferenceAutocompleteWidget
- class \Drupal\Core\Field\WidgetBase implements WidgetInterface, ContainerFactoryPluginInterface uses AllowedTagsXssTrait
- class \Drupal\Core\Field\PluginSettingsBase implements DependentPluginInterface, PluginSettingsInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of EntityqueueDragtableWidget
File
- src/
Plugin/ Field/ FieldWidget/ EntityqueueDragtableWidget.php, line 29
Namespace
Drupal\entityqueue\Plugin\Field\FieldWidgetView source
class EntityqueueDragtableWidget extends EntityReferenceAutocompleteWidget {
/**
* The unique HTML ID of the widget's wrapping element.
*
* @var string
*/
protected $wrapperId;
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return [
'link_to_entity' => FALSE,
'link_to_edit_form' => TRUE,
] + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements = parent::settingsForm($form, $form_state);
$elements['link_to_entity'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Link label to the referenced entity'),
'#default_value' => $this
->getSetting('link_to_entity'),
];
$elements['link_to_edit_form'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Add a link to the edit form of the referenced entity'),
'#default_value' => $this
->getSetting('link_to_edit_form'),
];
return $elements;
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = parent::settingsSummary();
$settings = $this
->getSettings();
if (!empty($settings['link_to_entity'])) {
$summary[] = $this
->t('Link to the referenced entity');
}
if (!empty($settings['link_to_edit_form'])) {
$summary[] = $this
->t('Link to the edit form of the referenced entity');
}
return $summary;
}
/**
* {@inheritdoc}
*/
public function form(FieldItemListInterface $items, array &$form, FormStateInterface $form_state, $get_delta = NULL) {
if ($this->fieldDefinition
->getTargetEntityTypeId() === 'entity_subqueue' && $this->fieldDefinition
->getName() === 'items') {
// Restrict the cardinality of the 'items' field if the queue has defined
// a maximum number of items and it is not configured to act as a queue.
/** @var \Drupal\entityqueue\EntityQueueInterface $queue */
$queue = $items
->getEntity()
->getQueue();
if (($max_size = $queue
->getMaximumSize()) && !$queue
->getActAsQueue()) {
$this->fieldDefinition
->getFieldStorageDefinition()
->setCardinality($max_size);
}
}
return parent::form($items, $form, $form_state, $get_delta);
}
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
assert($items instanceof EntityReferenceFieldItemListInterface);
/** @var \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository */
$entity_repository = \Drupal::service('entity.repository');
$referenced_entities = $items
->referencedEntities();
$field_name = $this->fieldDefinition
->getName();
if (isset($referenced_entities[$delta])) {
$entity = $entity_repository
->getTranslationFromContext($referenced_entities[$delta]);
$entity_label = $this
->getSetting('link_to_entity') && !$entity
->isNew() ? $entity
->toLink()
->toString() : $entity
->label();
$element += [
'#type' => 'container',
'#attributes' => [
'class' => [
'form--inline',
],
],
'target_id' => [
'#type' => 'item',
'#markup' => $entity
->access('view label') ? $entity_label : $this
->t('- Restricted access -'),
'#default_value' => !$referenced_entities[$delta]
->isNew() ? $referenced_entities[$delta]
->id() : NULL,
'#weight' => 0,
],
'_remove' => [
'#type' => 'submit',
'#name' => implode('_', array_merge($form['#parents'], [
$field_name,
$delta,
])) . '_remove',
'#delta' => $delta,
'#value' => $this
->t('Remove'),
'#attributes' => [
'class' => [
'remove-item-submit',
'align-right',
],
],
'#limit_validation_errors' => [
array_merge($form['#parents'], [
$field_name,
]),
],
'#submit' => [
[
get_class($this),
'removeSubmit',
],
],
'#ajax' => [
'callback' => [
get_class($this),
'removeAjax',
],
'wrapper' => $this
->getWrapperId(),
'effect' => 'fade',
],
],
];
// Show a link to the edit form of the entity if the entity type is
// editable.
if ($this
->getSetting('link_to_edit_form') && $referenced_entities[$delta]
->getEntityType()
->hasLinkTemplate('edit-form')) {
$element['_edit'] = $referenced_entities[$delta]
->toLink($this
->t('Edit'), 'edit-form', [
'query' => [
'destination' => \Drupal::request()
->getRequestUri(),
],
])
->toRenderable() + [
'#attributes' => [
'class' => [
'form-item',
'entityqueue-edit-item-link',
],
],
];
$element['#attached']['html_head'][] = [
[
'#tag' => 'style',
'#value' => '.js-form-wrapper .form-wrapper .form-item.entityqueue-edit-item-link { margin-left: 1em }',
],
'entityqueue-edit-item-link',
];
}
}
return $element;
}
/**
* {@inheritdoc}
*/
public function errorElement(array $element, ConstraintViolationInterface $violation, array $form, FormStateInterface $form_state) {
if ($button = $form_state
->getTriggeringElement()) {
// If an item is being removed, do not check for reference validity.
if (end($button['#parents']) === '_remove' && $violation
->getConstraint() instanceof ValidReferenceConstraint) {
return FALSE;
}
}
return parent::errorElement($element, $violation, $form, $form_state);
}
/**
* Submission handler for the "Remove" button.
*/
public static function removeSubmit(array &$form, FormStateInterface $form_state) {
$button = $form_state
->getTriggeringElement();
// Go two levels up in the form, to the widgets container.
$element = NestedArray::getValue($form, array_slice($button['#array_parents'], 0, -2));
$field_name = $element['#field_name'];
$parents = $element['#field_parents'];
// Set a NULL target ID for the removed element.
$form_state
->setValueForElement($element[$button['#delta']]['target_id'], NULL);
// Update the field item list values.
$items = $form_state
->getFormObject()
->getEntity()
->get($field_name);
$widget = $form_state
->getFormObject()
->getFormDisplay($form_state)
->getRenderer($field_name);
$widget
->extractFormValues($items, $form, $form_state);
// Remove unneeded properties.
foreach ($items as $item) {
unset($item->_remove);
}
// Decrease the items count.
$field_state = static::getWidgetState($parents, $field_name, $form_state);
$field_state['items_count']--;
static::setWidgetState($parents, $field_name, $form_state, $field_state);
$form_state
->setRebuild();
}
/**
* Ajax callback for the "Remove" button.
*/
public static function removeAjax(array $form, FormStateInterface $form_state) {
$button = $form_state
->getTriggeringElement();
// Go two levels up in the form, to the widget container.
$element = NestedArray::getValue($form, array_slice($button['#array_parents'], 0, -2));
return $element;
}
/**
* {@inheritdoc}
*/
protected function formMultipleElements(FieldItemListInterface $items, array &$form, FormStateInterface $form_state) {
$field_name = $this->fieldDefinition
->getName();
$cardinality = $this->fieldDefinition
->getFieldStorageDefinition()
->getCardinality();
$parents = $form['#parents'];
// Assign a unique identifier to each widget.
$id_prefix = implode('-', array_merge($parents, [
$field_name,
]));
$wrapper_id = Html::getUniqueId($id_prefix . '-wrapper');
$this
->setWrapperId($wrapper_id);
// Determine the number of widgets to display.
$field_state = static::getWidgetState($parents, $field_name, $form_state);
$max = $field_state['items_count'];
$elements = [];
if ($max > 0) {
for ($delta = 0; $delta < $max; $delta++) {
// For multiple fields, title and description are handled by the wrapping
// table.
$element = [
'#title' => $this
->t('@title (value @number)', [
'@title' => $this->fieldDefinition
->getLabel(),
'@number' => $delta + 1,
]),
'#title_display' => 'invisible',
'#description' => '',
];
$element = $this
->formSingleElement($items, $delta, $element, $form, $form_state);
if ($element) {
// We name the element '_weight' to avoid clashing with elements
// defined by widget.
$element['_weight'] = [
'#type' => 'weight',
'#title' => $this
->t('Weight for row @number', [
'@number' => $delta + 1,
]),
'#title_display' => 'invisible',
// Note: this 'delta' is the FAPI #type 'weight' element's property.
'#delta' => $max,
'#default_value' => $items[$delta]->_weight ?: $delta,
'#weight' => 100,
];
$elements[$delta] = $element;
}
}
}
$elements += [
'#theme' => 'field_multiple_value_form',
'#field_name' => $field_name,
'#cardinality' => $cardinality,
'#cardinality_multiple' => TRUE,
'#required' => $this->fieldDefinition
->isRequired(),
'#title' => $this->fieldDefinition
->getLabel(),
'#description' => FieldFilteredMarkup::create(\Drupal::token()
->replace($this->fieldDefinition
->getDescription())),
'#max_delta' => count($items) - 1,
'#prefix' => '<div id="' . $this
->getWrapperId() . '">',
'#suffix' => '</div>',
];
// Add 'add more' button, if not working with a programmed form.
if (($cardinality === FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED || $field_state['items_count'] < $cardinality) && !$form_state
->isProgrammed()) {
$elements['add_more'] = [
'#type' => 'container',
'#tree' => TRUE,
'#attributes' => [
'class' => [
'form--inline',
],
],
'new_item' => parent::formElement($items, -1, [], $form, $form_state),
'submit' => [
'#type' => 'submit',
'#name' => strtr($id_prefix, '-', '_') . '_add_more',
'#value' => $this
->t('Add item'),
'#attributes' => [
'class' => [
'field-add-more-submit',
],
],
'#limit_validation_errors' => [
array_merge($parents, [
$field_name,
]),
],
'#submit' => [
[
get_class($this),
'addItemSubmit',
],
],
'#ajax' => [
'callback' => [
get_class($this),
'addItemAjax',
],
'wrapper' => $this
->getWrapperId(),
'effect' => 'fade',
],
],
];
}
return $elements;
}
/**
* Submission handler for the "Add item" button.
*/
public static function addItemSubmit(array $form, FormStateInterface $form_state) {
$button = $form_state
->getTriggeringElement();
// Go two levels up in the form, to the widgets container.
$element = NestedArray::getValue($form, array_slice($button['#array_parents'], 0, -2));
$field_name = $element['#field_name'];
$parents = $element['#field_parents'];
$submitted_values = NestedArray::getValue($form_state
->getValues(), array_slice($button['#parents'], 0, -1));
if (isset($submitted_values['new_item']['target_id'])) {
$items = $form_state
->getFormObject()
->getEntity()
->get($field_name);
$items
->appendItem($submitted_values['new_item']);
// Increment the items count.
$field_state = static::getWidgetState($parents, $field_name, $form_state);
$field_state['items_count']++;
static::setWidgetState($parents, $field_name, $form_state, $field_state);
}
$form_state
->setRebuild();
}
/**
* Ajax callback for the "Add item" button.
*/
public static function addItemAjax(array $form, FormStateInterface $form_state) {
$button = $form_state
->getTriggeringElement();
// Go two levels up in the form, to the widgets container.
$element = NestedArray::getValue($form, array_slice($button['#array_parents'], 0, -2));
// Remove the submitted value from the 'Add item' textfield.
$element['add_more']['new_item']['target_id']['#value'] = NULL;
// Add a DIV around the delta receiving the Ajax effect.
$delta = $element['#max_delta'];
$element[$delta]['#prefix'] = '<div class="ajax-new-content">' . (isset($element[$delta]['#prefix']) ? $element[$delta]['#prefix'] : '');
$element[$delta]['#suffix'] = (isset($element[$delta]['#suffix']) ? $element[$delta]['#suffix'] : '') . '</div>';
return $element;
}
/**
* Sets the unique HTML ID of the widget's wrapping element.
*
* @param string $wrapperId
* The unique HTML ID.
*/
public function setWrapperId($wrapperId) {
if (!$this->wrapperId) {
$this->wrapperId = $wrapperId;
}
}
/**
* Gets the unique HTML ID of the widget's wrapping element.
*
* @return string
* The unique HTML ID.
*/
public function getWrapperId() {
return $this->wrapperId;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AllowedTagsXssTrait:: |
public | function | Returns a list of tags allowed by AllowedTagsXssTrait::fieldFilterXss(). | |
AllowedTagsXssTrait:: |
public | function | Returns a human-readable list of allowed tags for display in help texts. | |
AllowedTagsXssTrait:: |
public | function | Filters an HTML string to prevent XSS vulnerabilities. | |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
EntityqueueDragtableWidget:: |
protected | property | The unique HTML ID of the widget's wrapping element. | |
EntityqueueDragtableWidget:: |
public static | function | Ajax callback for the "Add item" button. | |
EntityqueueDragtableWidget:: |
public static | function | Submission handler for the "Add item" button. | |
EntityqueueDragtableWidget:: |
public static | function |
Defines the default settings for this plugin. Overrides EntityReferenceAutocompleteWidget:: |
|
EntityqueueDragtableWidget:: |
public | function |
Assigns a field-level validation error to the right widget sub-element. Overrides EntityReferenceAutocompleteWidget:: |
|
EntityqueueDragtableWidget:: |
public | function |
Creates a form element for a field. Overrides WidgetBase:: |
|
EntityqueueDragtableWidget:: |
public | function |
Returns the form for a single field widget. Overrides EntityReferenceAutocompleteWidget:: |
|
EntityqueueDragtableWidget:: |
protected | function |
Special handling to create form elements for multiple values. Overrides WidgetBase:: |
|
EntityqueueDragtableWidget:: |
public | function | Gets the unique HTML ID of the widget's wrapping element. | |
EntityqueueDragtableWidget:: |
public static | function | Ajax callback for the "Remove" button. | |
EntityqueueDragtableWidget:: |
public static | function | Submission handler for the "Remove" button. | |
EntityqueueDragtableWidget:: |
public | function |
Returns a form to configure settings for the widget. Overrides EntityReferenceAutocompleteWidget:: |
|
EntityqueueDragtableWidget:: |
public | function |
Returns a short summary for the current widget settings. Overrides EntityReferenceAutocompleteWidget:: |
|
EntityqueueDragtableWidget:: |
public | function | Sets the unique HTML ID of the widget's wrapping element. | |
EntityReferenceAutocompleteWidget:: |
protected | function | Returns the name of the bundle which will be used for autocreated entities. | |
EntityReferenceAutocompleteWidget:: |
protected | function | Returns the options for the match operator. | |
EntityReferenceAutocompleteWidget:: |
protected | function | Returns the value of a setting for the entity reference selection handler. | |
EntityReferenceAutocompleteWidget:: |
public | function |
Massages the form values into the format expected for field values. Overrides WidgetBase:: |
1 |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginSettingsBase:: |
protected | property | Whether default settings have been merged into the current $settings. | |
PluginSettingsBase:: |
protected | property | The plugin settings injected by third party modules. | |
PluginSettingsBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
6 |
PluginSettingsBase:: |
public | function |
Returns the value of a setting, or its default value if absent. Overrides PluginSettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Returns the array of settings, including defaults for missing settings. Overrides PluginSettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Gets the list of third parties that store information. Overrides ThirdPartySettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Gets the value of a third-party setting. Overrides ThirdPartySettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Gets all third-party settings of a given module. Overrides ThirdPartySettingsInterface:: |
|
PluginSettingsBase:: |
protected | function | Merges default settings values into $settings. | |
PluginSettingsBase:: |
public | function |
Informs the plugin that some configuration it depends on will be deleted. Overrides PluginSettingsInterface:: |
3 |
PluginSettingsBase:: |
public | function |
Sets the value of a setting for the plugin. Overrides PluginSettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Sets the settings for the plugin. Overrides PluginSettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Sets the value of a third-party setting. Overrides ThirdPartySettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Unsets a third-party setting. Overrides ThirdPartySettingsInterface:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
WidgetBase:: |
protected | property | The field definition. | |
WidgetBase:: |
protected | property |
The widget settings. Overrides PluginSettingsBase:: |
|
WidgetBase:: |
public static | function | Ajax callback for the "Add another item" button. | |
WidgetBase:: |
public static | function | Submission handler for the "Add another item" button. | |
WidgetBase:: |
public static | function | After-build handler for field elements in a form. | |
WidgetBase:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
5 |
WidgetBase:: |
public | function |
Extracts field values from submitted form values. Overrides WidgetBaseInterface:: |
2 |
WidgetBase:: |
public | function |
Reports field-level validation errors against actual form elements. Overrides WidgetBaseInterface:: |
2 |
WidgetBase:: |
protected | function | Generates the form element for a single copy of the widget. | |
WidgetBase:: |
protected | function | Returns the value of a field setting. | |
WidgetBase:: |
protected | function | Returns the array of field settings. | |
WidgetBase:: |
protected | function | Returns the filtered field description. | |
WidgetBase:: |
public static | function |
Retrieves processing information about the widget from $form_state. Overrides WidgetBaseInterface:: |
|
WidgetBase:: |
protected static | function | Returns the location of processing information within $form_state. | |
WidgetBase:: |
protected | function | Returns whether the widget handles multiple values. | |
WidgetBase:: |
public static | function |
Returns if the widget can be used for the provided field. Overrides WidgetInterface:: |
4 |
WidgetBase:: |
protected | function | Returns whether the widget used for default value form. | |
WidgetBase:: |
public static | function |
Stores processing information about the widget in $form_state. Overrides WidgetBaseInterface:: |
|
WidgetBase:: |
public | function |
Constructs a WidgetBase object. Overrides PluginBase:: |
5 |