You are here

public function WebformTemplatesController::index in Webform 6.x

Same name and namespace in other branches
  1. 8.5 modules/webform_templates/src/Controller/WebformTemplatesController.php \Drupal\webform_templates\Controller\WebformTemplatesController::index()

Returns the webform templates index page.

Parameters

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

bool $manage: Manage templates.

Return value

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

1 string reference to 'WebformTemplatesController::index'
webform_templates.routing.yml in modules/webform_templates/webform_templates.routing.yml
modules/webform_templates/webform_templates.routing.yml

File

modules/webform_templates/src/Controller/WebformTemplatesController.php, line 60

Class

WebformTemplatesController
Provides route responses for webform templates.

Namespace

Drupal\webform_templates\Controller

Code

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

  // Handler autocomplete redirect.
  if ($keys && preg_match('#\\(([^)]+)\\)$#', $keys, $match)) {
    if ($webform = $this
      ->getWebformStorage()
      ->load($match[1])) {
      return new RedirectResponse($webform
        ->toUrl()
        ->setAbsolute(TRUE)
        ->toString());
    }
  }
  $header = [];
  $header['title'] = $this
    ->t('Title');
  $header['description'] = [
    'data' => $this
      ->t('Description'),
    'class' => [
      RESPONSIVE_PRIORITY_LOW,
    ],
  ];
  $header['category'] = [
    'data' => $this
      ->t('Category'),
    'class' => [
      RESPONSIVE_PRIORITY_LOW,
    ],
  ];
  if ($manage) {
    $header['owner'] = [
      'data' => $this
        ->t('Author'),
      'class' => [
        RESPONSIVE_PRIORITY_LOW,
      ],
    ];
  }
  $header['operations'] = [
    'data' => $this
      ->t('Operations'),
  ];
  $webforms = $this
    ->getTemplates($keys, $category);
  $rows = [];
  foreach ($webforms as $webform) {
    $route_parameters = [
      'webform' => $webform
        ->id(),
    ];
    $row['title'] = $webform
      ->toLink();
    $row['description']['data']['#markup'] = $webform
      ->get('description');
    $row['category']['data']['#markup'] = $webform
      ->get('category');
    if ($manage) {
      $row['owner'] = ($owner = $webform
        ->getOwner()) ? $owner
        ->toLink() : '';
      $operations = [];
      if ($webform
        ->access('update')) {
        $operations['edit'] = [
          'title' => $this
            ->t('Build'),
          'url' => $this
            ->ensureDestination($webform
            ->toUrl('edit-form')),
        ];
      }
      if ($webform
        ->access('submission_page')) {
        $operations['view'] = [
          'title' => $this
            ->t('View'),
          'url' => $webform
            ->toUrl('canonical'),
        ];
      }
      if ($webform
        ->access('update')) {
        $operations['settings'] = [
          'title' => $this
            ->t('Settings'),
          'url' => $webform
            ->toUrl('settings'),
        ];
      }
      if ($webform
        ->access('duplicate')) {
        $operations['duplicate'] = [
          'title' => $this
            ->t('Duplicate'),
          'url' => $webform
            ->toUrl('duplicate-form', [
            'query' => [
              'template' => 1,
            ],
          ]),
          'attributes' => WebformDialogHelper::getModalDialogAttributes(WebformDialogHelper::DIALOG_NARROW),
        ];
      }
      if ($webform
        ->access('delete') && $webform
        ->hasLinkTemplate('delete-form')) {
        $operations['delete'] = [
          'title' => $this
            ->t('Delete'),
          'url' => $this
            ->ensureDestination($webform
            ->toUrl('delete-form')),
          'attributes' => WebformDialogHelper::getModalDialogAttributes(WebformDialogHelper::DIALOG_NARROW),
        ];
      }
      $row['operations']['data'] = [
        '#type' => 'operations',
        '#links' => $operations,
        '#prefix' => '<div class="webform-dropbutton">',
        '#suffix' => '</div>',
      ];
    }
    else {
      $row['operations']['data']['select'] = [
        '#type' => 'link',
        '#title' => $this
          ->t('Select'),
        '#url' => Url::fromRoute('entity.webform.duplicate_form', $route_parameters),
        '#attributes' => WebformDialogHelper::getModalDialogAttributes(WebformDialogHelper::DIALOG_NARROW, [
          'button',
          'button--primary',
        ]),
      ];
      $row['operations']['data']['preview'] = [
        '#type' => 'link',
        '#title' => $this
          ->t('Preview'),
        '#url' => Url::fromRoute('entity.webform.preview', $route_parameters),
        '#attributes' => WebformDialogHelper::getModalDialogAttributes(WebformDialogHelper::DIALOG_NORMAL, [
          'button',
        ]),
      ];
    }
    $rows[] = $row;
  }
  $build = [];
  $build['filter_form'] = $this->formBuilder
    ->getForm('\\Drupal\\webform_templates\\Form\\WebformTemplatesFilterForm', $keys);

  // Display info.
  if ($total = count($rows)) {
    $build['info'] = [
      '#markup' => $this
        ->formatPlural($total, '@count template', '@count templates'),
      '#prefix' => '<div>',
      '#suffix' => '</div>',
    ];
  }
  $build['table'] = [
    '#type' => 'table',
    '#header' => $header,
    '#rows' => $rows,
    '#sticky' => TRUE,
    '#empty' => $this
      ->t('There are no templates available.'),
    '#cache' => [
      'contexts' => $this
        ->getWebformStorage()
        ->getEntityType()
        ->getListCacheContexts(),
      'tags' => $this
        ->getWebformStorage()
        ->getEntityType()
        ->getListCacheTags(),
    ],
  ];

  // Must preload libraries required by (modal) dialogs.
  WebformDialogHelper::attachLibraries($build);
  return $build;
}