You are here

class WebformActions in Webform 8.5

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

Provides a wrapper element to group one or more Webform buttons in a form.

Plugin annotation

@RenderElement("webform_actions");

Hierarchy

Expanded class hierarchy of WebformActions

See also

\Drupal\Core\Render\Element\Actions

1 file declares its use of WebformActions
WebformActions.php in src/Plugin/WebformElement/WebformActions.php
3 #type uses of WebformActions
WebformSettingsPreviewTest::testPreview in tests/src/Functional/Settings/WebformSettingsPreviewTest.php
Tests webform webform submission form preview.
WebformWizardAdvancedTest::testAdvancedWizard in tests/src/Functional/Wizard/WebformWizardAdvancedTest.php
Test webform advanced wizard.
_webform_update_8046_convert_data in includes/webform.install.update.inc
Convert webform config data from settings.buttons to use the 'webform_actions' element.

File

src/Element/WebformActions.php, line 19

Namespace

Drupal\webform\Element
View source
class WebformActions extends Container {
  public static $buttons = [
    'submit',
    'reset',
    'delete',
    'draft',
    'wizard_prev',
    'wizard_next',
    'preview_prev',
    'preview_next',
  ];

  /**
   * {@inheritdoc}
   */
  public function getInfo() {
    $class = get_class($this);
    return [
      '#process' => [
        [
          $class,
          'processWebformActions',
        ],
        [
          $class,
          'processContainer',
        ],
      ],
      '#theme_wrappers' => [
        'container',
      ],
    ];
  }

  /**
   * Processes a form actions container element.
   *
   * @param array $element
   *   An associative array containing the properties and children of the
   *   form actions container.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   * @param array $complete_form
   *   The complete form structure.
   *
   * @return array
   *   The processed element.
   */
  public static function processWebformActions(&$element, FormStateInterface $form_state, &$complete_form) {

    /** @var \Drupal\webform\WebformSubmissionForm $form_object */
    $form_object = $form_state
      ->getFormObject();

    /** @var \Drupal\webform\WebformSubmissionInterface $webform_submission */
    $webform_submission = $form_object
      ->getEntity();
    $prefix = $element['#webform_key'] ? 'edit-' . $element['#webform_key'] . '-' : '';

    // Add class names only if form['actions']['#type'] is set to 'actions'.
    if (WebformElementHelper::isType($complete_form['actions'], 'actions')) {
      $element['#attributes']['class'][] = 'form-actions';
      $element['#attributes']['class'][] = 'webform-actions';
    }

    // Copy the form's actions to this element.
    $element += $complete_form['actions'];

    // Custom processing for the delete (link) action.
    if (isset($element['delete'])) {

      // Clone the URL so that each delete URL can have custom attributes.
      $element['delete']['#url'] = clone $element['delete']['#url'];

      // Add dialog attributes to the delete button.
      if (!empty($element['#delete__dialog'])) {
        $element['delete'] += [
          '#attributes' => [],
        ];
        $element['delete']['#attributes'] += [
          'class' => [],
        ];
        $dialog_attributes = WebformDialogHelper::getModalDialogAttributes(WebformDialogHelper::DIALOG_NARROW, $element['delete']['#attributes']['class']);
        $element['delete']['#attributes'] += $dialog_attributes;
        $element['delete']['#attributes']['class'] = $dialog_attributes['class'];
        WebformDialogHelper::attachLibraries($element);
      }

      // Restore access checking to the delete button.
      // @see \Drupal\webform\WebformSubmissionForm::actions
      if (isset($element['#delete_hide']) && $element['#delete_hide'] === FALSE) {
        $element['delete']['#access'] = $webform_submission
          ->access('delete');
        unset($element['#delete_hide']);
      }
    }

    // Track if buttons are visible.
    $has_visible_button = FALSE;
    foreach (static::$buttons as $button_name) {

      // Make sure the button exists.
      if (!isset($element[$button_name])) {
        continue;
      }

      // Get settings name.
      // The 'submit' button is used for creating and updating submissions.
      $is_update_button = $button_name === 'submit' && !($webform_submission
        ->isNew() || $webform_submission
        ->isDraft());
      $settings_name = $is_update_button ? 'update' : $button_name;

      // Set unique id for each button.
      if ($prefix) {
        $element[$button_name]['#id'] = Html::getUniqueId("{$prefix}{$button_name}");
      }

      // Hide buttons using #access.
      if (!empty($element['#' . $settings_name . '_hide'])) {
        $element[$button_name]['#access'] = FALSE;
      }

      // Apply custom label.
      $has_custom_label = !empty($element[$button_name]['#webform_actions_button_custom']);
      if (!empty($element['#' . $settings_name . '__label']) && !$has_custom_label) {
        if (isset($element[$button_name]['#type']) && $element[$button_name]['#type'] === 'link') {
          $element[$button_name]['#title'] = $element['#' . $settings_name . '__label'];
        }
        else {
          $element[$button_name]['#value'] = $element['#' . $settings_name . '__label'];
        }
      }

      // Apply custom name when needed for multiple submit buttons with
      // the same label.
      // @see https://www.drupal.org/project/webform/issues/3069240
      if (!empty($element['#' . $settings_name . '__name'])) {
        $element[$button_name]['#name'] = $element['#' . $settings_name . '__name'];
      }

      // Apply attributes (class, style, properties).
      if (!empty($element['#' . $settings_name . '__attributes'])) {
        $element[$button_name] += [
          '#attributes' => [],
        ];
        foreach ($element['#' . $settings_name . '__attributes'] as $attribute_name => $attribute_value) {
          if ($attribute_name === 'class') {
            $element[$button_name]['#attributes'] += [
              'class' => [],
            ];

            // Merge class names.
            $element[$button_name]['#attributes']['class'] = array_merge($element[$button_name]['#attributes']['class'], $attribute_value);
          }
          else {
            $element[$button_name]['#attributes'][$attribute_name] = $attribute_value;
          }
        }
      }
      if (Element::isVisibleElement($element[$button_name])) {
        $has_visible_button = TRUE;
      }
    }

    // Hide form actions only if the element is accessible.
    // This prevents form from having no actions.
    if (Element::isVisibleElement($element)) {
      $complete_form['actions']['#access'] = FALSE;
    }

    // Hide actions element if no buttons are visible (i.e. #access = FALSE).
    if (!$has_visible_button) {
      $element['#access'] = FALSE;
    }
    return $element;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Container::preRenderContainer public static function Prevents optional containers from rendering if they have no children.
Container::processContainer public static function Processes a container element.
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
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.
WebformActions::$buttons public static property
WebformActions::getInfo public function Returns the element properties for this element. Overrides Container::getInfo
WebformActions::processWebformActions public static function Processes a form actions container element.