You are here

public function YamlFormPluginHandlerController::listHandlers in YAML Form 8

Shows a list of form handlers that can be added to a form.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The current request.

\Drupal\yamlform\YamlFormInterface $yamlform: A form.

Return value

array A render array as expected by the renderer.

1 string reference to 'YamlFormPluginHandlerController::listHandlers'
yamlform.routing.yml in ./yamlform.routing.yml
yamlform.routing.yml

File

src/Controller/YamlFormPluginHandlerController.php, line 94

Class

YamlFormPluginHandlerController
Controller for all form handlers.

Namespace

Drupal\yamlform\Controller

Code

public function listHandlers(Request $request, YamlFormInterface $yamlform) {
  $headers = [
    [
      'data' => $this
        ->t('Handler'),
      'width' => '20%',
    ],
    [
      'data' => $this
        ->t('Description'),
      'width' => '40%',
    ],
    [
      'data' => $this
        ->t('Category'),
      'width' => '20%',
    ],
    [
      'data' => $this
        ->t('Operations'),
      'width' => '20%',
    ],
  ];
  $definitions = $this->pluginManager
    ->getDefinitions();
  $definitions = $this->pluginManager
    ->getSortedDefinitions($definitions);
  $rows = [];
  foreach ($definitions as $plugin_id => $definition) {

    // Skip email handler which has dedicated button.
    if ($plugin_id == 'email') {
      continue;
    }

    // Check cardinality.
    $cardinality = $definition['cardinality'];
    $is_cardinality_unlimited = $cardinality == YamlFormHandlerInterface::CARDINALITY_UNLIMITED;
    $is_cardinality_reached = $yamlform
      ->getHandlers($plugin_id)
      ->count() >= $cardinality;
    if (!$is_cardinality_unlimited && $is_cardinality_reached) {
      continue;
    }
    $row = [];
    $row['title']['data'] = [
      '#type' => 'inline_template',
      '#template' => '<div class="yamlform-form-filter-text-source">{{ label }}</div>',
      '#context' => [
        'label' => $definition['label'],
      ],
    ];
    $row['description'] = [
      'data' => [
        '#markup' => $definition['description'],
      ],
    ];
    $row['category'] = $definition['category'];
    $links['add'] = [
      'title' => $this
        ->t('Add handler'),
      'url' => Url::fromRoute('entity.yamlform.handler.add_form', [
        'yamlform' => $yamlform
          ->id(),
        'yamlform_handler' => $plugin_id,
      ]),
      'attributes' => YamlFormDialogHelper::getModalDialogAttributes(800),
    ];
    $row['operations']['data'] = [
      '#type' => 'operations',
      '#links' => $links,
    ];
    $rows[] = $row;
  }
  $build['#attached']['library'][] = 'yamlform/yamlform.form';
  $build['filter'] = [
    '#type' => 'search',
    '#title' => $this
      ->t('Filter'),
    '#title_display' => 'invisible',
    '#size' => 30,
    '#placeholder' => $this
      ->t('Filter by handler name'),
    '#attributes' => [
      'class' => [
        'yamlform-form-filter-text',
      ],
      'data-element' => '.yamlform-handler-add-table',
      'title' => $this
        ->t('Enter a part of the handler name to filter by.'),
      'autofocus' => 'autofocus',
    ],
  ];
  $build['handlers'] = [
    '#type' => 'table',
    '#header' => $headers,
    '#rows' => $rows,
    '#empty' => $this
      ->t('No handler available.'),
    '#attributes' => [
      'class' => [
        'yamlform-handler-add-table',
      ],
    ],
  ];
  return $build;
}