You are here

public function WebformEntityHandlersForm::form in Webform 8.5

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

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/WebformEntityHandlersForm.php, line 61

Class

WebformEntityHandlersForm
Provides a webform to manage submission handlers.

Namespace

Drupal\webform

Code

public function form(array $form, FormStateInterface $form_state) {
  $user_input = $form_state
    ->getUserInput();

  // Hard code the form id.
  $form['#id'] = 'webform-handlers-form';

  // Build table header.
  $header = [
    [
      'data' => $this
        ->t('Title / Description'),
    ],
    [
      'data' => $this
        ->t('ID'),
      'class' => [
        RESPONSIVE_PRIORITY_LOW,
      ],
    ],
    [
      'data' => $this
        ->t('Summary'),
      'class' => [
        RESPONSIVE_PRIORITY_LOW,
      ],
    ],
    [
      'data' => $this
        ->t('Status'),
      'class' => [
        RESPONSIVE_PRIORITY_LOW,
      ],
    ],
    [
      'data' => $this
        ->t('Weight'),
      'class' => [
        'webform-tabledrag-hide',
      ],
    ],
    [
      'data' => $this
        ->t('Operations'),
    ],
  ];

  // Build table rows for handlers.
  $handlers = $this->entity
    ->getHandlers();
  $rows = [];
  foreach ($handlers as $handler_id => $handler) {
    $offcanvas_dialog_attributes = WebformDialogHelper::getOffCanvasDialogAttributes($handler
      ->getOffCanvasWidth());
    $row['#attributes']['class'][] = 'draggable';
    $row['#attributes']['data-webform-key'] = $handler_id;
    $row['#weight'] = isset($user_input['handlers']) && isset($user_input['handlers'][$handler_id]) ? $user_input['handlers'][$handler_id]['weight'] : NULL;
    $row['handler'] = [
      '#tree' => FALSE,
      'data' => [
        'label' => [
          '#type' => 'link',
          '#title' => $handler
            ->label(),
          '#url' => Url::fromRoute('entity.webform.handler.edit_form', [
            'webform' => $this->entity
              ->id(),
            'webform_handler' => $handler_id,
          ]),
          '#attributes' => $offcanvas_dialog_attributes,
        ],
        'description' => [
          '#prefix' => '<br/>',
          '#markup' => $handler
            ->getNotes() ?: $handler
            ->description(),
        ],
      ],
    ];
    $row['id'] = [
      'data' => [
        '#markup' => $handler
          ->getHandlerId(),
      ],
    ];
    $row['summary'] = $handler
      ->getSummary();
    if ($handler
      ->isDisabled()) {
      $status = $this
        ->t('Disabled');
    }
    else {
      $status = $handler
        ->supportsConditions() && $handler
        ->getConditions() ? $this
        ->t('Conditional') : $this
        ->t('Enabled');
    }
    $row['status'] = [
      'data' => [
        '#markup' => $status,
      ],
    ];
    $row['weight'] = [
      '#type' => 'weight',
      '#title' => $this
        ->t('Weight for @title', [
        '@title' => $handler
          ->label(),
      ]),
      '#title_display' => 'invisible',
      '#delta' => 50,
      '#default_value' => $handler
        ->getWeight(),
      '#attributes' => [
        'class' => [
          'webform-handler-order-weight',
        ],
      ],
      '#wrapper_attributes' => [
        'class' => [
          'webform-tabledrag-hide',
        ],
      ],
    ];
    $operations = [];

    // Edit.
    $operations['edit'] = [
      'title' => $this
        ->t('Edit'),
      'url' => Url::fromRoute('entity.webform.handler.edit_form', [
        'webform' => $this->entity
          ->id(),
        'webform_handler' => $handler_id,
      ]),
      'attributes' => $offcanvas_dialog_attributes,
    ];

    // Duplicate.
    if ($handler
      ->cardinality() === WebformHandlerInterface::CARDINALITY_UNLIMITED) {
      $operations['duplicate'] = [
        'title' => $this
          ->t('Duplicate'),
        'url' => Url::fromRoute('entity.webform.handler.duplicate_form', [
          'webform' => $this->entity
            ->id(),
          'webform_handler' => $handler_id,
        ]),
        'attributes' => $offcanvas_dialog_attributes,
      ];
    }

    // Test individual handler.
    if ($this->entity
      ->access('test')) {
      $operations['test'] = [
        'title' => $this
          ->t('Test'),
        'url' => Url::fromRoute('entity.webform.test_form', [
          'webform' => $this->entity
            ->id(),
        ], [
          'query' => [
            '_webform_handler' => $handler_id,
          ],
        ]),
      ];
    }

    // Add AJAX functionality to enable/disable operations.
    $operations['status'] = [
      'title' => $handler
        ->isEnabled() ? $this
        ->t('Disable') : $this
        ->t('Enable'),
      'url' => Url::fromRoute('entity.webform.handler.' . ($handler
        ->isEnabled() ? 'disable' : 'enable'), [
        'webform' => $this->entity
          ->id(),
        'webform_handler' => $handler_id,
      ]),
      'attributes' => WebformDialogHelper::getModalDialogAttributes(WebformDialogHelper::DIALOG_NARROW, [
        'use-ajax',
      ]),
    ];

    // Delete.
    $operations['delete'] = [
      'title' => $this
        ->t('Delete'),
      'url' => Url::fromRoute('entity.webform.handler.delete_form', [
        'webform' => $this->entity
          ->id(),
        'webform_handler' => $handler_id,
      ]),
      'attributes' => WebformDialogHelper::getModalDialogAttributes(WebformDialogHelper::DIALOG_NARROW),
    ];
    $row['operations'] = [
      '#type' => 'operations',
      '#links' => $operations,
      '#prefix' => '<div class="webform-dropbutton">',
      '#suffix' => '</div>',
    ];
    $rows[$handler_id] = $row;
  }

  // Build the list of existing webform handlers for this webform.
  $form['handlers'] = [
    '#type' => 'table',
    '#header' => $header,
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'webform-handler-order-weight',
      ],
    ],
    '#attributes' => [
      'id' => 'webform-handlers',
      'class' => [
        'webform-handlers-table',
      ],
    ],
    '#empty' => $this
      ->t('There are currently no handlers setup for this webform.'),
  ] + $rows;

  // Must preload libraries required by (modal) dialogs.
  WebformDialogHelper::attachLibraries($form);
  $form['#attached']['library'][] = 'webform/webform.admin.tabledrag';
  return parent::form($form, $form_state);
}