You are here

class WebformTableSelectSort in Webform 8.5

Same name in this branch
  1. 8.5 src/Element/WebformTableSelectSort.php \Drupal\webform\Element\WebformTableSelectSort
  2. 8.5 src/Plugin/WebformElement/WebformTableSelectSort.php \Drupal\webform\Plugin\WebformElement\WebformTableSelectSort
Same name and namespace in other branches
  1. 6.x src/Element/WebformTableSelectSort.php \Drupal\webform\Element\WebformTableSelectSort

Provides a webform element for a sortable tableselect element.

Plugin annotation

@FormElement("webform_tableselect_sort");

Hierarchy

Expanded class hierarchy of WebformTableSelectSort

5 #type uses of WebformTableSelectSort
DateList::form in src/Plugin/WebformElement/DateList.php
Gets the actual configuration webform array to be built.
WebformAdminConfigBaseForm::buildBulkOperations in src/Form/AdminConfig/WebformAdminConfigBaseForm.php
Build bulk operation settings for webforms and submissions.
WebformEntitySettingsSubmissionsForm::form in src/EntitySettings/WebformEntitySettingsSubmissionsForm.php
Gets the actual form array to be built.
WebformExampleCustomFormSettingsForm::buildForm in modules/webform_example_custom_form/src/Form/WebformExampleCustomFormSettingsForm.php
Form constructor.
WebformResultsCustomForm::buildForm in src/Form/WebformResultsCustomForm.php
Form constructor.

File

src/Element/WebformTableSelectSort.php, line 16

Namespace

Drupal\webform\Element
View source
class WebformTableSelectSort extends Table {

  /**
   * {@inheritdoc}
   */
  public function getInfo() {
    $class = get_class($this);
    return [
      '#input' => TRUE,
      '#js_select' => TRUE,
      '#responsive' => TRUE,
      '#sticky' => FALSE,
      '#pre_render' => [
        [
          $class,
          'preRenderTable',
        ],
        [
          $class,
          'preRenderWebformTableSelectSort',
        ],
      ],
      '#process' => [
        [
          $class,
          'processWebformTableSelectSort',
        ],
      ],
      '#options' => [],
      '#empty' => '',
      '#theme' => 'table__tableselect_sort',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
    if ($input === FALSE) {
      $value = [];
      $element += [
        '#default_value' => [],
      ];
      foreach ($element['#default_value'] as $key => $flag) {
        if ($flag) {
          $value[$key] = $key;
        }
      }
      return $value;
    }
    else {
      if (is_array($input)) {
        uasort($input, [
          'Drupal\\Component\\Utility\\SortArray',
          'sortByWeightElement',
        ]);
        $values = [];
        foreach ($input as $key => $item) {
          if (!empty($item['checkbox'])) {
            $values[$item['checkbox']] = $item['checkbox'];
          }
        }
        return $values;
      }
      else {
        return [];
      }
    }
  }

  /**
   * Prepares a 'webform_tableselect_sort' #type element for rendering.
   *
   * @return array
   *   The processed element.
   */
  public static function preRenderWebformTableSelectSort($element) {
    $rows = [];
    $header = $element['#header'];
    if (!empty($element['#options'])) {

      // Generate a table row for each selectable item in #options.
      foreach (Element::children($element) as $key) {
        $row = [];
        $row['data'] = [];

        // Set the row to be draggable.
        $row['class'] = [
          'draggable',
        ];
        if (isset($element['#options'][$key]['#attributes'])) {
          $row += $element['#options'][$key]['#attributes'];
        }

        // Render the checkbox element.
        $row['data'][] = \Drupal::service('renderer')
          ->render($element[$key]['checkbox']);

        // As table.html.twig only maps header and row columns by order, create
        // the correct order by iterating over the header fields.
        foreach ($element['#header'] as $fieldname => $title) {

          // A row cell can span over multiple headers, which means less row
          // cells than headers could be present.
          if (isset($element['#options'][$key][$fieldname])) {

            // A header can span over multiple cells and in this case the cells
            // are passed in an array. The order of this array determines the
            // order in which they are added.
            if (is_array($element['#options'][$key][$fieldname]) && !isset($element['#options'][$key][$fieldname]['data'])) {
              foreach ($element['#options'][$key][$fieldname] as $cell) {
                $row['data'][] = $cell;
              }
            }
            else {
              $row['data'][] = $element['#options'][$key][$fieldname];
            }
          }
        }

        // Render the weight element.
        $row['data'][] = \Drupal::service('renderer')
          ->render($element[$key]['weight']);
        $rows[] = $row;
      }

      // Add an empty header or a "Select all" checkbox to provide room for the
      // checkboxes in the first table column.
      if ($element['#js_select']) {

        // Add a "Select all" checkbox.
        $element['#attached']['library'][] = 'core/drupal.tableselect';
        array_unshift($header, [
          'class' => [
            'select-all',
          ],
        ]);
      }
      else {

        // Add an empty header when radio buttons are displayed or a "Select all"
        // checkbox is not desired.
        array_unshift($header, '');
      }
    }

    // Append weight to header.
    $header[] = t('Weight');

    // Set header and rows.
    $element['#header'] = $header;
    $element['#rows'] = $rows;

    // Attach table select UX improvements.
    $element['#attributes']['class'][] = 'webform-tableselect';
    $element['#attributes']['class'][] = 'js-webform-tableselect';
    $element['#attached']['library'][] = 'webform/webform.element.tableselect';

    // Attach table sort.
    $element['#attributes']['class'][] = 'js-tableselect-sort';
    $element['#attributes']['class'][] = 'tableselect-sort';
    $element['#attached']['library'][] = 'webform/webform.element.tableselect_sort';
    return $element;
  }

  /**
   * Creates checkbox and weights to populate a 'webform_tableselect_order' table.
   *
   * @param array $element
   *   An associative array containing the properties and children of the
   *   webform_tableselect_order element.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   * @param array $complete_form
   *   The complete webform structure.
   *
   * @return array
   *   The processed element.
   */
  public static function processWebformTableSelectSort(&$element, FormStateInterface $form_state, &$complete_form) {
    $value = is_array($element['#value']) ? $element['#value'] : [];

    // Add validate callback that extracts the associative array of options.
    $element += [
      '#element_validate' => [],
    ];
    array_unshift($element['#element_validate'], [
      get_called_class(),
      'validateWebformTableSelectOrder',
    ]);
    $element['#tree'] = TRUE;
    if (count($element['#options']) > 0) {
      if (!isset($element['#default_value']) || $element['#default_value'] === 0) {
        $element['#default_value'] = [];
      }

      // Place checked options first.
      $options = [];
      foreach ($value as $checked_option_key) {
        if (isset($element['#options'][$checked_option_key])) {
          $options[$checked_option_key] = $element['#options'][$checked_option_key];
          unset($element['#options'][$checked_option_key]);
        }
      }
      $options += $element['#options'];
      $element['#options'] = $options;

      // Set delta and default weight.
      $delta = count($element['#options']);
      $weight = 0;

      // Create a checkbox and weight for each item in #options in such a way
      // that the value of the webform_tableselect_order element behaves as if
      // it had been of type checkboxes with weight.
      foreach ($element['#options'] as $key => $choice) {

        // Do not overwrite manually created children.
        if (!isset($element[$key])) {
          if ($title = TableSelect::getTableSelectOptionTitle($choice)) {
            $checkbox_title = $title;
            $weight_title = new TranslatableMarkup('Weight for @title', [
              '@title' => $title,
            ]);
          }
          else {
            $checkbox_title = '';
            $weight_title = '';
          }
          $element[$key]['checkbox'] = [
            '#type' => 'checkbox',
            '#title' => $checkbox_title,
            '#title_display' => 'invisible',
            '#return_value' => $key,
            '#default_value' => isset($value[$key]) ? $key : NULL,
            '#attributes' => $element['#attributes'],
            '#ajax' => isset($element['#ajax']) ? $element['#ajax'] : NULL,
          ];
          $element[$key]['weight'] = [
            '#type' => 'weight',
            '#title' => $weight_title,
            '#title_display' => 'invisible',
            '#delta' => $delta,
            '#default_value' => $weight++,
            '#attributes' => [
              'class' => [
                'table-sort-weight',
              ],
            ],
          ];
        }
      }
    }
    else {
      $element['#value'] = [];
    }

    // Enable tabledrag.
    $element['#tabledrag'] = [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'table-sort-weight',
      ],
    ];
    return $element;
  }

  /**
   * Validates webform_tableselect_other.
   */
  public static function validateWebformTableSelectOrder(&$element, FormStateInterface $form_state, &$complete_form) {

    // Get and sort checked values.
    $checked_values = [];
    foreach (Element::children($element) as $key) {
      if ($element[$key]['checkbox']['#value']) {
        $checked_values[] = [
          'value' => $element[$key]['checkbox']['#value'],
          'weight' => $element[$key]['weight']['#value'],
        ];
      }
    }
    uasort($checked_values, [
      'Drupal\\Component\\Utility\\SortArray',
      'sortByWeightElement',
    ]);

    // Set values.
    $values = [];
    foreach ($checked_values as $item) {
      $values[$item['value']] = $item['value'];
    }

    // Clear the element's value by setting it to NULL.
    $form_state
      ->setValueForElement($element, NULL);

    // Now, set the values as the element's value.
    $element['#value'] = $values;
    $form_state
      ->setValueForElement($element, $values);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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
FormElement::processAutocomplete public static function Adds autocomplete functionality to elements.
FormElement::processPattern public static function #process callback for #pattern form element property.
FormElement::validatePattern public static function #element_validate callback for #pattern form element property.
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.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92
RenderElement::preRenderAjaxForm public static function Adds Ajax information about an element to communicate with JavaScript.
RenderElement::preRenderGroup public static function Adds members of this group as actual elements for rendering.
RenderElement::processAjaxForm public static function Form element processing handler for the #ajax form property. 1
RenderElement::processGroup public static function Arranges elements into groups.
RenderElement::setAttributes public static function Sets a form element's class attribute. Overrides ElementInterface::setAttributes
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.
Table::preRenderTable public static function #pre_render callback to transform children of an element of #type 'table'.
Table::processTable public static function #process callback for #type 'table' to add tableselect support.
Table::validateTable public static function #element_validate callback for #type 'table'.
WebformTableSelectSort::getInfo public function Returns the element properties for this element. Overrides Table::getInfo
WebformTableSelectSort::preRenderWebformTableSelectSort public static function Prepares a 'webform_tableselect_sort' #type element for rendering.
WebformTableSelectSort::processWebformTableSelectSort public static function Creates checkbox and weights to populate a 'webform_tableselect_order' table.
WebformTableSelectSort::validateWebformTableSelectOrder public static function Validates webform_tableselect_other.
WebformTableSelectSort::valueCallback public static function Determines how user input is mapped to an element's #value property. Overrides Table::valueCallback