You are here

protected function WebformUiEntityElementsForm::getElementRow in Webform 6.x

Same name and namespace in other branches
  1. 8.5 modules/webform_ui/src/WebformUiEntityElementsForm.php \Drupal\webform_ui\WebformUiEntityElementsForm::getElementRow()

Gets an row for a single element.

Parameters

array $element: Webform element.

int $delta: The number of elements.

array $parent_options: An associative array of parent (container) options.

Return value

array The row for the element.

1 call to WebformUiEntityElementsForm::getElementRow()
WebformUiEntityElementsForm::buildForm in modules/webform_ui/src/WebformUiEntityElementsForm.php
Form constructor.

File

modules/webform_ui/src/WebformUiEntityElementsForm.php, line 435

Class

WebformUiEntityElementsForm
Webform manage elements UI form.

Namespace

Drupal\webform_ui

Code

protected function getElementRow(array $element, $delta, array $parent_options) {

  /** @var \Drupal\webform\WebformInterface $webform */
  $webform = $this
    ->getEntity();
  $row = [];
  $element_state_options = OptGroup::flattenOptions(WebformElementStates::getStateOptions());
  $key = $element['#webform_key'];
  $title = $element['#admin_title'] ?: $element['#title'];
  $title = is_array($title) ? $this->renderer
    ->render($title) : $title;
  $plugin_id = $this->elementManager
    ->getElementPluginId($element);

  /** @var \Drupal\webform\Plugin\WebformElementInterface $webform_element */
  $webform_element = $this->elementManager
    ->createInstance($plugin_id);
  $offcanvas_dialog_attributes = WebformDialogHelper::getOffCanvasDialogAttributes($webform_element
    ->getOffCanvasWidth());
  $is_container = $webform_element
    ->isContainer($element);
  $is_root = $webform_element
    ->isRoot();
  $is_element_disabled = $webform_element
    ->isDisabled();
  $is_access_disabled = !Element::isVisibleElement($element);

  // If disabled, display warning.
  if ($is_element_disabled) {
    $webform_element
      ->displayDisabledWarning($element);
  }

  // Get row class names.
  $row_class = [
    'draggable',
  ];
  if ($is_root) {
    $row_class[] = 'tabledrag-root';
    $row_class[] = 'webform-ui-element-root';
  }
  if (!$is_container) {
    $row_class[] = 'tabledrag-leaf';
  }
  if ($is_container) {
    $row_class[] = 'webform-ui-element-container';
  }
  if (!empty($element['#type'])) {
    $row_class[] = 'webform-ui-element-type-' . $element['#type'];
  }
  else {
    $row_class[] = 'webform-ui-element-container';
  }
  if ($is_element_disabled || $is_access_disabled) {
    $row_class[] = 'webform-ui-element-disabled';
  }

  // Add element key and type.
  $row['#attributes']['data-webform-key'] = $element['#webform_key'];
  $row['#attributes']['data-webform-type'] = isset($element['#type']) ? $element['#type'] : '';
  $row['#attributes']['class'] = $row_class;
  $indentation = NULL;
  if ($element['#webform_depth']) {
    $indentation = [
      '#theme' => 'indentation',
      '#size' => $element['#webform_depth'],
    ];
  }
  $row['title'] = [
    'link' => [
      '#type' => 'link',
      '#title' => $element['#admin_title'] ?: $element['#title'],
      '#url' => new Url('entity.webform_ui.element.edit_form', [
        'webform' => $webform
          ->id(),
        'key' => $key,
      ]),
      '#attributes' => $offcanvas_dialog_attributes,
      '#prefix' => !empty($indentation) ? $this->renderer
        ->renderPlain($indentation) : '',
    ],
  ];
  if (!empty($element['#admin_notes'])) {
    $row['title']['notes'] = [
      '#type' => 'webform_help',
      '#help_title' => $element['#admin_title'] ?: $element['#title'],
      '#help' => $element['#admin_notes'],
      '#weight' => 100,
    ];
  }
  if ($webform
    ->hasContainer()) {
    if ($is_container) {
      $route_parameters = [
        'webform' => $webform
          ->id(),
      ];
      $route_options = [
        'query' => [
          'parent' => $key,
        ],
      ];
      if ($webform_element instanceof WebformTable) {
        $route_parameters['type'] = 'webform_table_row';
        $row['add'] = [
          '#type' => 'link',
          '#title' => $this
            ->t('Add <span>row</span>'),
          '#url' => new Url('entity.webform_ui.element.add_form', $route_parameters, $route_options),
          '#attributes' => WebformDialogHelper::getOffCanvasDialogAttributes(WebformDialogHelper::DIALOG_NORMAL, [
            'button',
            'button-action',
            'button--primary',
            'button--small',
          ]),
        ];
      }
      else {
        $row['add'] = [
          '#type' => 'link',
          '#title' => $this
            ->t('Add <span>element</span>'),
          '#url' => new Url('entity.webform_ui.element', $route_parameters, $route_options),
          '#attributes' => WebformDialogHelper::getModalDialogAttributes(WebformDialogHelper::DIALOG_NORMAL, [
            'button',
            'button-action',
            'button--primary',
            'button--small',
          ]),
        ];
      }
    }
    else {
      $row['add'] = [
        '#markup' => '',
      ];
    }
  }
  $row['name'] = [
    '#markup' => $element['#webform_key'],
  ];
  $type = $webform_element
    ->getPluginLabel();
  if ($webform_element instanceof WebformElement) {
    if (!empty($element['#type'])) {
      $type = '[' . $element['#type'] . ']';
    }
    elseif (isset($element['#theme'])) {
      $type = '[' . $element['#theme'] . ']';
    }
  }
  $row['type'] = [
    '#markup' => $type,
  ];
  if ($webform
    ->hasFlexboxLayout()) {
    $row['flex'] = [
      '#markup' => empty($element['#flex']) ? 1 : $element['#flex'],
    ];
  }
  $is_conditionally_required = FALSE;
  if ($webform
    ->hasConditions()) {
    $states = [];
    if (!empty($element['#states'])) {
      $states = array_intersect_key($element_state_options, $element['#states']);
      $is_conditionally_required = array_intersect_key($this->requiredStates, $element['#states']);
    }
    $row['conditional'] = [
      '#type' => 'link',
      '#title' => implode('; ', $states),
      '#url' => new Url('entity.webform_ui.element.edit_form', [
        'webform' => $webform
          ->id(),
        'key' => $key,
      ]),
      '#attributes' => $offcanvas_dialog_attributes + [
        // Add custom hash to current page's location.
        // @see Drupal.behaviors.webformAjaxLink
        'data-hash' => 'webform-tab--conditions',
        'title' => $this
          ->t('Edit @states conditional', [
          '@states' => implode('; ', $states),
        ]),
        'aria-label' => $this
          ->t('Edit @states conditional', [
          '@states' => implode('; ', $states),
        ]),
      ],
    ];
  }
  if ($webform_element
    ->hasProperty('required')) {
    $row['required'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Required for @title', [
        '@title' => $title,
      ]),
      '#title_display' => 'invisible',
      '#default_value' => empty($element['#required']) ? FALSE : TRUE,
    ];
    if ($is_conditionally_required) {
      $row['required']['#default_value'] = TRUE;
      $row['required']['#disabled'] = TRUE;
    }
  }
  else {
    $row['required'] = [
      '#markup' => '',
    ];
  }
  $row['weight'] = [
    '#type' => 'weight',
    '#title' => $this
      ->t('Weight for @title', [
      '@title' => $title,
    ]),
    '#title_display' => 'invisible',
    '#default_value' => $element['#weight'],
    '#wrapper_attributes' => [
      'class' => [
        'webform-tabledrag-hide',
      ],
    ],
    '#attributes' => [
      'class' => [
        'row-weight',
      ],
    ],
    '#delta' => $delta,
  ];
  $row['parent'] = [
    '#wrapper_attributes' => [
      'class' => [
        'webform-tabledrag-hide',
      ],
    ],
  ];
  $row['parent']['key'] = [
    '#parents' => [
      'webform_ui_elements',
      $key,
      'key',
    ],
    '#type' => 'hidden',
    '#value' => $key,
    '#attributes' => [
      'class' => [
        'row-key',
      ],
    ],
  ];
  if ($parent_options) {
    $row['parent']['parent_key'] = [
      '#parents' => [
        'webform_ui_elements',
        $key,
        'parent_key',
      ],
      '#type' => 'select',
      '#options' => $parent_options,
      '#empty_option' => '',
      '#title' => $this
        ->t('Parent element @title', [
        '@title' => $title,
      ]),
      '#title_display' => 'invisible',
      '#default_value' => $element['#webform_parent_key'],
      '#attributes' => [
        'class' => [
          'row-parent-key',
        ],
      ],
    ];
  }
  else {
    $row['parent']['parent_key'] = [
      '#parents' => [
        'webform_ui_elements',
        $key,
        'parent_key',
      ],
      '#type' => 'hidden',
      '#default_value' => '',
      '#attributes' => [
        'class' => [
          'row-parent-key',
        ],
      ],
    ];
  }
  $row['operations'] = [
    '#type' => 'operations',
    '#prefix' => '<div class="webform-dropbutton">',
    '#suffix' => '</div>',
  ];
  $row['operations']['#links']['edit'] = [
    'title' => $this
      ->t('Edit'),
    'url' => new Url('entity.webform_ui.element.edit_form', [
      'webform' => $webform
        ->id(),
      'key' => $key,
    ]),
    'attributes' => $offcanvas_dialog_attributes,
  ];

  // Issue #2741877 Nested modals don't work: when using CKEditor in a
  // modal, then clicking the image button opens another modal,
  // which closes the original modal.
  // @todo Remove the below workaround once this issue is resolved.
  if ($webform_element
    ->getPluginId() === 'processed_text' && !WebformDialogHelper::useOffCanvas()) {
    unset($row['operations']['#links']['edit']['attributes']);
  }
  if (!$is_container) {
    $row['operations']['#links']['duplicate'] = [
      'title' => $this
        ->t('Duplicate'),
      'url' => new Url('entity.webform_ui.element.duplicate_form', [
        'webform' => $webform
          ->id(),
        'key' => $key,
      ]),
      'attributes' => $offcanvas_dialog_attributes,
    ];
  }
  $row['operations']['#links']['delete'] = [
    'title' => $this
      ->t('Delete'),
    'url' => new Url('entity.webform_ui.element.delete_form', [
      'webform' => $webform
        ->id(),
      'key' => $key,
    ]),
    'attributes' => WebformDialogHelper::getModalDialogAttributes(WebformDialogHelper::DIALOG_NARROW),
  ];
  return $row;
}