You are here

class WebformSubmissionViews in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Element/WebformSubmissionViews.php \Drupal\webform\Element\WebformSubmissionViews

Provides a form element for selecting webform submission views.

Plugin annotation

@FormElement("webform_submission_views");

Hierarchy

Expanded class hierarchy of WebformSubmissionViews

2 #type uses of WebformSubmissionViews
WebformAdminConfigSubmissionsForm::buildForm in src/Form/AdminConfig/WebformAdminConfigSubmissionsForm.php
Form constructor.
WebformEntitySettingsSubmissionsForm::form in src/EntitySettings/WebformEntitySettingsSubmissionsForm.php
Gets the actual form array to be built.

File

src/Element/WebformSubmissionViews.php, line 14

Namespace

Drupal\webform\Element
View source
class WebformSubmissionViews extends WebformMultiple {

  /**
   * {@inheritdoc}
   */
  public static function processWebformMultiple(&$element, FormStateInterface $form_state, &$complete_form) {
    if (!\Drupal::moduleHandler()
      ->moduleExists('views')) {
      $element['#element_validate'] = [
        [
          get_called_class(),
          'emptyValue',
        ],
      ];
      return $element;
    }
    $element['#key'] = 'name';
    $element['#header'] = TRUE;
    $element['#empty_items'] = 0;
    $element['#min_items'] = 1;
    $element['#add_more_input_label'] = t('more submission views');

    // Build element.
    $element['#element'] = [];

    // Name / Title / View.
    $view_options = [];

    /** @var \Drupal\views\ViewEntityInterface[] $views */
    $views = View::loadMultiple();
    foreach ($views as $view) {

      // Only include webform submission views.
      if ($view
        ->get('base_table') !== 'webform_submission' || $view
        ->get('base_field') !== 'sid') {
        continue;
      }
      $optgroup = $view
        ->label();
      $displays = $view
        ->get('display');
      foreach ($displays as $display_id => $display) {

        // Only include embed displays.
        if ($display['display_plugin'] === 'embed') {
          $view_options[$optgroup][$view
            ->id() . ':' . $display_id] = $optgroup . ': ' . $display['display_title'];
        }
      }
    }
    $element['#element']['name_title_view'] = [
      '#type' => 'container',
      '#title' => t('View / Name / Title'),
      '#help' => '<b>' . t('View') . ':</b> ' . t('A webform submission embed display. The selected view should also include contextual filters. {webform_id}/{source_entity_type}/{source_entity_id}/{account_id}/{in_draft}') . '<hr/>' . '<b>' . t('Name') . ':</b> ' . t('The name to be displayed in the URL when there are multiple submission views available.') . '<hr/>' . '<b>' . t('Options') . ':</b> ' . t('The title to be display in the dropdown menu when there are multiple submission views available.'),
      'view' => [
        '#type' => 'select',
        '#title' => t('View'),
        '#title_display' => 'invisible',
        '#empty_option' => t('Select view…'),
        '#options' => $view_options,
        '#error_no_message' => TRUE,
      ],
      'name' => [
        '#type' => 'textfield',
        '#title' => t('Name'),
        '#title_display' => 'invisible',
        '#placeholder' => t('Enter name…'),
        '#size' => 20,
        '#pattern' => '^[-_a-z0-9]+$',
        '#error_no_message' => TRUE,
      ],
      'title' => [
        '#type' => 'textfield',
        '#title' => t('Title'),
        '#title_display' => 'invisible',
        '#placeholder' => t('Enter title…'),
        '#size' => 20,
        '#error_no_message' => TRUE,
      ],
    ];

    // Global routes.
    if (!empty($element['#global'])) {
      $global_route_options = [
        'entity.webform_submission.collection' => t('Submissions'),
        'entity.webform_submission.user' => t('User'),
      ];
      $element['#element']['global_routes'] = [
        '#type' => 'checkboxes',
        '#title' => t('Apply to global'),
        '#help' => t('Display the selected view on the below paths') . '<hr/><b>' . t('Submissions') . ':</b><br/>/admin/structure/webform/submissions/manage' . '<hr/><b>' . t('User') . ':</b><br/>/user/{user}/submissions',
        '#options' => $global_route_options,
        '#element_validate' => [
          [
            '\\Drupal\\webform\\Utility\\WebformElementHelper',
            'filterValues',
          ],
        ],
        '#error_no_message' => TRUE,
      ];
    }

    // Webform routes.
    $webform_route_options = [
      'entity.webform.results_submissions' => t('Submissions'),
      'entity.webform.user.drafts' => t('User drafts'),
      'entity.webform.user.submissions' => t('User submissions'),
    ];
    $element['#element']['webform_routes'] = [
      '#type' => 'checkboxes',
      '#title' => t('Apply to webform'),
      '#help' => t('Display the selected view on the below paths') . '<hr/><b>' . t('Submissions') . ':</b><br/>/admin/structure/webform/manage/{webform}/results/submissions' . '<hr/><b>' . t('User drafts') . ':</b><br/>/webform/{webform}/drafts' . '<hr/><b>' . t('User submissions') . ':</b><br/>/webform/{webform}/submissions',
      '#options' => $webform_route_options,
      '#element_validate' => [
        [
          '\\Drupal\\webform\\Utility\\WebformElementHelper',
          'filterValues',
        ],
      ],
      '#error_no_message' => TRUE,
    ];

    // Node routes.
    if (\Drupal::moduleHandler()
      ->moduleExists('webform_node')) {
      $node_route_options = [
        'entity.node.webform.results_submissions' => t('Submissions'),
        'entity.node.webform.user.drafts' => t('User drafts'),
        'entity.node.webform.user.submissions' => t('User submissions'),
      ];
      $element['#element']['node_routes'] = [
        '#type' => 'checkboxes',
        '#title' => t('Apply to node'),
        '#help' => t('Display the selected view on the below paths') . '<hr/><b>' . t('Submissions') . ':</b><br/>/node/{node}/webform/results/submissions' . '<hr/>' . '<b>' . t('User drafts') . ':</b><br/>/node/{node}/webform/drafts' . '<hr/>' . '<b>' . t('User submissions') . ':</b><br/>/node/{node}/webform/submissions',
        '#options' => $node_route_options,
        '#element_validate' => [
          [
            '\\Drupal\\webform\\Utility\\WebformElementHelper',
            'filterValues',
          ],
        ],
        '#error_no_message' => TRUE,
      ];
    }
    parent::processWebformMultiple($element, $form_state, $complete_form);
    return $element;
  }

  /**
   * {@inheritdoc}
   */
  public static function validateWebformMultiple(&$element, FormStateInterface $form_state, &$complete_form) {
    if (!\Drupal::moduleHandler()
      ->moduleExists('views')) {
      $element['#value'] = [];
      $form_state
        ->setValueForElement($element, []);
      return;
    }
    parent::validateWebformMultiple($element, $form_state, $complete_form);
    $items = NestedArray::getValue($form_state
      ->getValues(), $element['#parents']);
    foreach ($items as $name => &$item) {

      // Remove empty view references.
      if ($name === '' && empty($item['view']) && empty($item['global_routes']) && empty($item['webform_routes']) && empty($item['node_routes'])) {
        unset($items[$name]);
        continue;
      }
      if ($name === '') {
        $form_state
          ->setError($element, t('Name is required.'));
      }
      if (empty($item['title'])) {
        $form_state
          ->setError($element, t('Title is required.'));
      }
      if (empty($item['view'])) {
        $form_state
          ->setError($element, t('View name/display id is required.'));
      }
    }
    $element['#value'] = $items;
    $form_state
      ->setValueForElement($element, $items);
  }

  /**
   * Form validate callback which clears the submitted value.
   */
  public static function emptyValue(&$element, FormStateInterface $form_state, &$complete_form) {
    $element['#value'] = [];
    $form_state
      ->setValueForElement($element, []);
  }

}

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.
WebformMultiple::addItemsSubmit public static function Webform submission handler for adding more items.
WebformMultiple::addItemSubmit public static function Webform submission handler for adding an item.
WebformMultiple::ajaxCallback public static function Webform submission Ajax callback the returns the list table.
WebformMultiple::buildElementHeader protected static function Build a single element header.
WebformMultiple::buildElementRow protected static function Build a single element row.
WebformMultiple::buildElementTitle protected static function Build an element's title with help.
WebformMultiple::CARDINALITY_UNLIMITED constant Value indicating a element accepts an unlimited number of values.
WebformMultiple::convertValuesToItems public static function Convert an array containing of values (elements or _item_ and weight) to an array of items.
WebformMultiple::convertValueToItem public static function Convert value array containing (elements or _item_ and weight) to an item.
WebformMultiple::getInfo public function Returns the element properties for this element. Overrides ElementInterface::getInfo
WebformMultiple::getStorageKey public static function Get unique key used to store the number of items for an element.
WebformMultiple::hasRequireElement protected static function Determine if any sub-element is required.
WebformMultiple::initializeElement protected static function Initialize element.
WebformMultiple::initializeElementRecursive protected static function Initialize, prepare, and finalize composite sub-elements recursively.
WebformMultiple::isEmpty public static function Check if array is empty.
WebformMultiple::isHidden protected static function Determine if an element is hidden.
WebformMultiple::removeItemSubmit public static function Webform submission handler for removing an item.
WebformMultiple::setElementDefaultValue protected static function Set element row default value recursively.
WebformMultiple::setElementRowDefaultValueRecursive protected static function Set element row default value recursively.
WebformMultiple::setElementRowParentsRecursive protected static function Set element row parents recursively.
WebformMultiple::validateUniqueKeys protected static function Validate composite element has unique keys.
WebformMultiple::valueCallback public static function Determines how user input is mapped to an element's #value property. Overrides FormElement::valueCallback
WebformSubmissionViews::emptyValue public static function Form validate callback which clears the submitted value.
WebformSubmissionViews::processWebformMultiple public static function Process items and build multiple elements widget. Overrides WebformMultiple::processWebformMultiple
WebformSubmissionViews::validateWebformMultiple public static function Validates webform multiple element. Overrides WebformMultiple::validateWebformMultiple