You are here

protected function WebformTemplatesController::getTemplates 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::getTemplates()

Get webform templates.

Parameters

string $keys: (optional) Filter templates by keyword.

string $category: (optional) Filter templates by category.

Return value

array|\Drupal\Core\Entity\EntityInterface[] An array webform entity that are used as templates.

1 call to WebformTemplatesController::getTemplates()
WebformTemplatesController::index in modules/webform_templates/src/Controller/WebformTemplatesController.php
Returns the webform templates index page.

File

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

Class

WebformTemplatesController
Provides route responses for webform templates.

Namespace

Drupal\webform_templates\Controller

Code

protected function getTemplates($keys = '', $category = '') {
  $query = $this
    ->getWebformStorage()
    ->getQuery();
  $query
    ->condition('template', TRUE);
  $query
    ->condition('archive', FALSE);

  // Filter by key(word).
  if ($keys) {
    $or = $query
      ->orConditionGroup()
      ->condition('title', $keys, 'CONTAINS')
      ->condition('description', $keys, 'CONTAINS')
      ->condition('category', $keys, 'CONTAINS')
      ->condition('elements', $keys, 'CONTAINS');
    $query
      ->condition($or);
  }

  // Filter by category.
  if ($category) {
    $query
      ->condition('category', $category);
  }
  $query
    ->sort('title');
  $entity_ids = $query
    ->execute();
  if (empty($entity_ids)) {
    return [];
  }

  /* @var $entities \Drupal\webform\WebformInterface[] */
  $entities = $this
    ->getWebformStorage()
    ->loadMultiple($entity_ids);

  // If the user is not a webform admin, check view access to each webform.
  if (!$this
    ->isAdmin()) {
    foreach ($entities as $entity_id => $entity) {
      if (!$entity
        ->access('view')) {
        unset($entities[$entity_id]);
      }
    }
  }
  return $entities;
}