You are here

public function View::form in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Plugin/WebformElement/View.php \Drupal\webform\Plugin\WebformElement\View::form()

Gets the actual configuration webform array to be built.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array An associative array contain the element's configuration webform without any default values.

Overrides WebformMarkupBase::form

File

src/Plugin/WebformElement/View.php, line 57

Class

View
Provides a hidden 'view' element.

Namespace

Drupal\webform\Plugin\WebformElement

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);
  $form['view'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('View settings'),
    '#open' => TRUE,
  ];
  $form['view']['view_message'] = [
    '#type' => 'webform_message',
    '#message_type' => 'warning',
    '#access' => TRUE,
    '#message_message' => $this
      ->t("View displays with exposed filters are not supported because exposed filters nest a <form> within a <form> and this breaks the webform."),
    '#message_close' => TRUE,
    '#message_storage' => WebformMessageElement::STORAGE_SESSION,
    '#states' => [
      'visible' => [
        ':input[name="properties[display_on]"]' => [
          '!value' => WebformElementDisplayOnInterface::DISPLAY_ON_VIEW,
        ],
      ],
    ],
  ];

  // Move display on from markup.
  $form['view']['display_on'] = $form['markup']['display_on'];
  $form['view']['name'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('View name'),
    '#required' => TRUE,
  ];
  $form['view']['display_id'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('View display id'),
    '#required' => TRUE,
  ];
  $form['view']['arguments'] = [
    '#type' => 'webform_multiple',
    '#title' => $this
      ->t('View arguments'),
  ];

  // If the view element is hidden, don't allow the view settings
  // to be updated.
  if ($this
    ->isHidden()) {

    /** @var \Drupal\webform_ui\Form\WebformUiElementEditForm $form_object */
    $form_object = $form_state
      ->getFormObject();
    $element = $form_object
      ->getElement();

    // Display message.
    $form['view']['view_message'] = [
      '#type' => 'webform_message',
      '#message_type' => 'info',
      '#message_message' => $this
        ->t("Only users who can 'Administer views' or 'Edit webform source code' can update the view name, display id, and arguments."),
      '#message_close' => TRUE,
      '#message_storage' => WebformMessageElement::STORAGE_SESSION,
      '#access' => TRUE,
    ];

    // Hide input and display values as items.
    $view_properties = [
      'name',
      'display_id',
      'arguments',
    ];
    foreach ($view_properties as $view_property) {
      $form['view'][$view_property]['#access'] = FALSE;
      if (!empty($element['#' . $view_property])) {
        $form['view'][$view_property . '_item'] = [
          '#type' => 'item',
          '#title' => $form['view'][$view_property]['#title'],
          '#markup' => is_array($element['#' . $view_property]) ? implode('/', $element['#' . $view_property]) : $element['#' . $view_property],
          '#access' => TRUE,
        ];
      }
    }
  }
  unset($form['markup']);
  return $form;
}