You are here

protected function YamlFormTemplatesController::getTemplates in YAML Form 8

Get form templates.

Parameters

string $keys: (optional) Filter templates by key word.

Return value

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

1 call to YamlFormTemplatesController::getTemplates()
YamlFormTemplatesController::index in modules/yamlform_templates/src/Controller/YamlFormTemplatesController.php
Returns the form templates index page.

File

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

Class

YamlFormTemplatesController
Provides route responses for form templates.

Namespace

Drupal\yamlform_templates\Controller

Code

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

  // Filter by key(word).
  if ($keys) {
    $or = $query
      ->orConditionGroup()
      ->condition('title', $keys, 'CONTAINS')
      ->condition('description', $keys, 'CONTAINS')
      ->condition('elements', $keys, 'CONTAINS');
    $query
      ->condition($or);
  }
  $query
    ->sort('title');
  $entity_ids = $query
    ->execute();
  if (empty($entity_ids)) {
    return [];
  }

  /* @var $entities \Drupal\yamlform\YamlFormInterface[] */
  $entities = $this->yamlformStorage
    ->loadMultiple($entity_ids);

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