You are here

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

Expanded class hierarchy of EntityqueueDragtableWidget

File

src/Plugin/Field/FieldWidget/EntityqueueDragtableWidget.php, line 29

Namespace

Drupal\entityqueue\Plugin\Field\FieldWidget
View 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

Namesort descending Modifiers Type Description Overrides
AllowedTagsXssTrait::allowedTags public function Returns a list of tags allowed by AllowedTagsXssTrait::fieldFilterXss().
AllowedTagsXssTrait::displayAllowedTags public function Returns a human-readable list of allowed tags for display in help texts.
AllowedTagsXssTrait::fieldFilterXss public function Filters an HTML string to prevent XSS vulnerabilities.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
EntityqueueDragtableWidget::$wrapperId protected property The unique HTML ID of the widget's wrapping element.
EntityqueueDragtableWidget::addItemAjax public static function Ajax callback for the "Add item" button.
EntityqueueDragtableWidget::addItemSubmit public static function Submission handler for the "Add item" button.
EntityqueueDragtableWidget::defaultSettings public static function Defines the default settings for this plugin. Overrides EntityReferenceAutocompleteWidget::defaultSettings
EntityqueueDragtableWidget::errorElement public function Assigns a field-level validation error to the right widget sub-element. Overrides EntityReferenceAutocompleteWidget::errorElement
EntityqueueDragtableWidget::form public function Creates a form element for a field. Overrides WidgetBase::form
EntityqueueDragtableWidget::formElement public function Returns the form for a single field widget. Overrides EntityReferenceAutocompleteWidget::formElement
EntityqueueDragtableWidget::formMultipleElements protected function Special handling to create form elements for multiple values. Overrides WidgetBase::formMultipleElements
EntityqueueDragtableWidget::getWrapperId public function Gets the unique HTML ID of the widget's wrapping element.
EntityqueueDragtableWidget::removeAjax public static function Ajax callback for the "Remove" button.
EntityqueueDragtableWidget::removeSubmit public static function Submission handler for the "Remove" button.
EntityqueueDragtableWidget::settingsForm public function Returns a form to configure settings for the widget. Overrides EntityReferenceAutocompleteWidget::settingsForm
EntityqueueDragtableWidget::settingsSummary public function Returns a short summary for the current widget settings. Overrides EntityReferenceAutocompleteWidget::settingsSummary
EntityqueueDragtableWidget::setWrapperId public function Sets the unique HTML ID of the widget's wrapping element.
EntityReferenceAutocompleteWidget::getAutocreateBundle protected function Returns the name of the bundle which will be used for autocreated entities.
EntityReferenceAutocompleteWidget::getMatchOperatorOptions protected function Returns the options for the match operator.
EntityReferenceAutocompleteWidget::getSelectionHandlerSetting protected function Returns the value of a setting for the entity reference selection handler.
EntityReferenceAutocompleteWidget::massageFormValues public function Massages the form values into the format expected for field values. Overrides WidgetBase::massageFormValues 1
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginSettingsBase::$defaultSettingsMerged protected property Whether default settings have been merged into the current $settings.
PluginSettingsBase::$thirdPartySettings protected property The plugin settings injected by third party modules.
PluginSettingsBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies 6
PluginSettingsBase::getSetting public function Returns the value of a setting, or its default value if absent. Overrides PluginSettingsInterface::getSetting
PluginSettingsBase::getSettings public function Returns the array of settings, including defaults for missing settings. Overrides PluginSettingsInterface::getSettings
PluginSettingsBase::getThirdPartyProviders public function Gets the list of third parties that store information. Overrides ThirdPartySettingsInterface::getThirdPartyProviders
PluginSettingsBase::getThirdPartySetting public function Gets the value of a third-party setting. Overrides ThirdPartySettingsInterface::getThirdPartySetting
PluginSettingsBase::getThirdPartySettings public function Gets all third-party settings of a given module. Overrides ThirdPartySettingsInterface::getThirdPartySettings
PluginSettingsBase::mergeDefaults protected function Merges default settings values into $settings.
PluginSettingsBase::onDependencyRemoval public function Informs the plugin that some configuration it depends on will be deleted. Overrides PluginSettingsInterface::onDependencyRemoval 3
PluginSettingsBase::setSetting public function Sets the value of a setting for the plugin. Overrides PluginSettingsInterface::setSetting
PluginSettingsBase::setSettings public function Sets the settings for the plugin. Overrides PluginSettingsInterface::setSettings
PluginSettingsBase::setThirdPartySetting public function Sets the value of a third-party setting. Overrides ThirdPartySettingsInterface::setThirdPartySetting
PluginSettingsBase::unsetThirdPartySetting public function Unsets a third-party setting. Overrides ThirdPartySettingsInterface::unsetThirdPartySetting
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
WidgetBase::$fieldDefinition protected property The field definition.
WidgetBase::$settings protected property The widget settings. Overrides PluginSettingsBase::$settings
WidgetBase::addMoreAjax public static function Ajax callback for the "Add another item" button.
WidgetBase::addMoreSubmit public static function Submission handler for the "Add another item" button.
WidgetBase::afterBuild public static function After-build handler for field elements in a form.
WidgetBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 5
WidgetBase::extractFormValues public function Extracts field values from submitted form values. Overrides WidgetBaseInterface::extractFormValues 2
WidgetBase::flagErrors public function Reports field-level validation errors against actual form elements. Overrides WidgetBaseInterface::flagErrors 2
WidgetBase::formSingleElement protected function Generates the form element for a single copy of the widget.
WidgetBase::getFieldSetting protected function Returns the value of a field setting.
WidgetBase::getFieldSettings protected function Returns the array of field settings.
WidgetBase::getFilteredDescription protected function Returns the filtered field description.
WidgetBase::getWidgetState public static function Retrieves processing information about the widget from $form_state. Overrides WidgetBaseInterface::getWidgetState
WidgetBase::getWidgetStateParents protected static function Returns the location of processing information within $form_state.
WidgetBase::handlesMultipleValues protected function Returns whether the widget handles multiple values.
WidgetBase::isApplicable public static function Returns if the widget can be used for the provided field. Overrides WidgetInterface::isApplicable 4
WidgetBase::isDefaultValueWidget protected function Returns whether the widget used for default value form.
WidgetBase::setWidgetState public static function Stores processing information about the widget in $form_state. Overrides WidgetBaseInterface::setWidgetState
WidgetBase::__construct public function Constructs a WidgetBase object. Overrides PluginBase::__construct 5