You are here

public function YamlFormEntityListBuilder::render in YAML Form 8

Builds the entity listing as renderable array for table.html.twig.

@todo Add a link to add a new item to the #empty text.

Overrides EntityListBuilder::render

File

src/YamlFormEntityListBuilder.php, line 64

Class

YamlFormEntityListBuilder
Defines a class to build a listing of form entities.

Namespace

Drupal\yamlform

Code

public function render() {

  // Handler autocomplete redirect.
  if ($this->keys && preg_match('#\\(([^)]+)\\)$#', $this->keys, $match)) {
    if ($yamlform = $this
      ->getStorage()
      ->load($match[1])) {
      return new RedirectResponse($yamlform
        ->toUrl()
        ->setAbsolute(TRUE)
        ->toString());
    }
  }
  $build = [];

  // Must manually add local actions to the form because we can't alter local
  // actions and add the needed dialog attributes.
  // @see https://www.drupal.org/node/2585169
  if ($this
    ->moduleHandler()
    ->moduleExists('yamlform_ui')) {
    $add_form_attributes = YamlFormDialogHelper::getModalDialogAttributes(640, [
      'button',
      'button-action',
      'button--primary',
      'button--small',
    ]);
  }
  else {
    $add_form_attributes = [
      'class' => [
        'button',
        'button-action',
        'button--primary',
        'button--small',
      ],
    ];
  }
  if (\Drupal::currentUser()
    ->hasPermission('create yamlform')) {
    $build['local_actions'] = [
      'add_form' => [
        '#type' => 'link',
        '#title' => $this
          ->t('Add form'),
        '#url' => new Url('entity.yamlform.add_form'),
        '#attributes' => $add_form_attributes,
      ],
    ];
  }

  // Add the filter by key(word) and/or state.
  $state_options = [
    '' => $this
      ->t('All [@total]', [
      '@total' => $this
        ->getTotal(NULL, NULL),
    ]),
    YamlFormEntityListBuilder::STATE_OPEN => $this
      ->t('Open [@total]', [
      '@total' => $this
        ->getTotal(NULL, self::STATE_OPEN),
    ]),
    YamlFormEntityListBuilder::STATE_CLOSED => $this
      ->t('Closed [@total]', [
      '@total' => $this
        ->getTotal(NULL, self::STATE_CLOSED),
    ]),
  ];
  $build['filter_form'] = \Drupal::formBuilder()
    ->getForm('\\Drupal\\yamlform\\Form\\YamlFormEntityFilterForm', $this->keys, $this->state, $state_options);

  // Display info.
  if ($this
    ->isAdmin()) {
    if ($total = $this
      ->getTotal($this->keys, $this->state)) {
      $t_args = [
        '@total' => $total,
        '@results' => $this
          ->formatPlural($total, $this
          ->t('form'), $this
          ->t('forms')),
      ];
      $build['info'] = [
        '#markup' => $this
          ->t('@total @results', $t_args),
        '#prefix' => '<div>',
        '#suffix' => '</div>',
      ];
    }
  }
  $build += parent::render();

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