You are here

public function YamlFormTemplatesController::index in YAML Form 8

Returns the form templates index page.

Parameters

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

Return value

array|RedirectResponse A render array representing the form templates index page or redirect response to a selected form via the filter's autocomplete.

1 string reference to 'YamlFormTemplatesController::index'
yamlform_templates.routing.yml in modules/yamlform_templates/yamlform_templates.routing.yml
modules/yamlform_templates/yamlform_templates.routing.yml

File

modules/yamlform_templates/src/Controller/YamlFormTemplatesController.php, line 81

Class

YamlFormTemplatesController
Provides route responses for form templates.

Namespace

Drupal\yamlform_templates\Controller

Code

public function index(Request $request) {
  $keys = $request
    ->get('search');

  // Handler autocomplete redirect.
  if ($keys && preg_match('#\\(([^)]+)\\)$#', $keys, $match)) {
    if ($yamlform = $this->yamlformStorage
      ->load($match[1])) {
      return new RedirectResponse($yamlform
        ->toUrl()
        ->setAbsolute(TRUE)
        ->toString());
    }
  }
  $header = [
    $this
      ->t('Title'),
    [
      'data' => $this
        ->t('Description'),
      'class' => [
        RESPONSIVE_PRIORITY_LOW,
      ],
    ],
    [
      'data' => $this
        ->t('Operations'),
      'colspan' => 2,
    ],
  ];
  $yamlforms = $this
    ->getTemplates($keys);
  $rows = [];
  foreach ($yamlforms as $yamlform) {
    $route_parameters = [
      'yamlform' => $yamlform
        ->id(),
    ];
    $row['title'] = $yamlform
      ->toLink();
    $row['description']['data']['description']['#markup'] = $yamlform
      ->get('description');
    if ($this->currentUser
      ->hasPermission('create yamlform')) {
      $row['select']['data'] = [
        '#type' => 'operations',
        '#links' => [
          'duplicate' => [
            'title' => $this
              ->t('Select'),
            'url' => Url::fromRoute('entity.yamlform.duplicate_form', $route_parameters),
            'attributes' => YamlFormDialogHelper::getModalDialogAttributes(640),
          ],
        ],
      ];
    }
    $row['preview']['data'] = [
      '#type' => 'operations',
      '#links' => [
        'preview' => [
          'title' => $this
            ->t('Preview'),
          'url' => Url::fromRoute('entity.yamlform.preview', $route_parameters),
          'attributes' => YamlFormDialogHelper::getModalDialogAttributes(800),
        ],
      ],
    ];
    $rows[] = $row;
  }
  $build = [];
  $build['filter_form'] = $this->formBuilder
    ->getForm('\\Drupal\\yamlform_templates\\Form\\YamlFormTemplatesFilterForm', $keys);
  $build['table'] = [
    '#type' => 'table',
    '#header' => $header,
    '#rows' => $rows,
    '#empty' => $this
      ->t('There are no templates available.'),
    '#cache' => [
      'contexts' => $this->yamlformStorage
        ->getEntityType()
        ->getListCacheContexts(),
      'tags' => $this->yamlformStorage
        ->getEntityType()
        ->getListCacheTags(),
    ],
  ];

  // Must preload libraries required by (modal) dialogs.
  $build['#attached']['library'][] = 'yamlform/yamlform.admin.dialog';
  return $build;
}