You are here

public function WebformPluginVariantController::listVariants in Webform 8.5

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

Shows a list of webform variants that can be added to a webform.

Parameters

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

\Drupal\webform\WebformInterface $webform: A webform.

Return value

array A render array as expected by the renderer.

1 string reference to 'WebformPluginVariantController::listVariants'
webform.routing.yml in ./webform.routing.yml
webform.routing.yml

File

src/Controller/WebformPluginVariantController.php, line 153

Class

WebformPluginVariantController
Controller for all webform variants.

Namespace

Drupal\webform\Controller

Code

public function listVariants(Request $request, WebformInterface $webform) {

  // Get enabled variant types.
  $elements = $webform
    ->getElementsVariant();
  $variant_types = [];
  foreach ($elements as $element_key) {
    $element = $webform
      ->getElement($element_key);
    if (isset($element['#variant'])) {
      $variant_types[$element['#variant']] = $element['#variant'];
    }
  }
  $headers = [
    [
      'data' => $this
        ->t('Variant'),
      '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);
  $definitions = $this->pluginManager
    ->removeExcludeDefinitions($definitions);
  $rows = [];
  foreach ($definitions as $plugin_id => $definition) {

    // Make sure variant type is enabled.
    if (!isset($variant_types[$plugin_id])) {
      continue;
    }

    /** @var \Drupal\webform\Plugin\WebformVariantInterface $variant_plugin */
    $variant_plugin = $this->pluginManager
      ->createInstance($plugin_id);
    $row = [];
    $row['title']['data'] = [
      '#type' => 'link',
      '#title' => $definition['label'],
      '#url' => Url::fromRoute('entity.webform.variant.add_form', [
        'webform' => $webform
          ->id(),
        'webform_variant' => $plugin_id,
      ]),
      '#attributes' => WebformDialogHelper::getOffCanvasDialogAttributes($variant_plugin
        ->getOffCanvasWidth()),
      '#prefix' => '<div class="webform-form-filter-text-source">',
      '#suffix' => '</div>',
    ];
    $row['description'] = [
      'data' => [
        '#markup' => $definition['description'],
      ],
    ];
    $row['category'] = $definition['category'];
    $links['add'] = [
      'title' => $this
        ->t('Add variant'),
      'url' => Url::fromRoute('entity.webform.variant.add_form', [
        'webform' => $webform
          ->id(),
        'webform_variant' => $plugin_id,
      ]),
      'attributes' => WebformDialogHelper::getOffCanvasDialogAttributes($variant_plugin
        ->getOffCanvasWidth()),
    ];
    $row['operations']['data'] = [
      '#type' => 'operations',
      '#links' => $links,
      '#prefix' => '<div class="webform-dropbutton">',
      '#suffix' => '</div>',
    ];
    $rows[] = $row;
  }
  $build['filter'] = [
    '#type' => 'search',
    '#title' => $this
      ->t('Filter'),
    '#title_display' => 'invisible',
    '#size' => 30,
    '#placeholder' => $this
      ->t('Filter by variant name'),
    '#attributes' => [
      'class' => [
        'webform-form-filter-text',
      ],
      'data-element' => '.webform-variant-add-table',
      'data-item-singlular' => $this
        ->t('variant'),
      'data-item-plural' => $this
        ->t('variants'),
      'title' => $this
        ->t('Enter a part of the variant name to filter by.'),
      'autofocus' => 'autofocus',
    ],
  ];
  $build['variants'] = [
    '#type' => 'table',
    '#header' => $headers,
    '#rows' => $rows,
    '#sticky' => TRUE,
    '#empty' => $this
      ->t('No variant available.'),
    '#attributes' => [
      'class' => [
        'webform-variant-add-table',
      ],
    ],
  ];
  $build['#attached']['library'][] = 'webform/webform.form';
  $build['#attached']['library'][] = 'webform/webform.filter';
  return $build;
}