You are here

public function OverviewMenuInstances::buildForm in Organic Groups Menu (OG Menu) 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/OverviewMenuInstances.php, line 60

Class

OverviewMenuInstances
Provides terms overview form for a taxonomy vocabulary.

Namespace

Drupal\og_menu\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, OgMenuInterface $ogmenu = NULL) {
  $header = [
    [
      'data' => $this
        ->t('Name'),
    ],
  ];
  $og_instance_storage = $this->entityTypeManager
    ->getStorage('ogmenu_instance');
  $query = $og_instance_storage
    ->getQuery()
    ->pager(50)
    ->sort('id')
    ->condition('type', $ogmenu
    ->id());
  $rids = $query
    ->execute();
  $entities = $og_instance_storage
    ->loadMultiple($rids);
  $rows = [];

  /** @var \Drupal\Core\Entity\EntityInterface $entity */
  foreach ($entities as $entity) {
    $value = $entity
      ->get(OgGroupAudienceHelperInterface::DEFAULT_FIELD)
      ->getValue();
    if (!$value) {
      throw new \Exception('OG Menu requires an og group to be referenced.');
    }
    $rows[] = [
      'data' => [
        $entity
          ->toLink()
          ->toString(),
      ],
    ];
  }
  $build['table'] = [
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $rows,
    '#empty' => $this
      ->t('No menu instances have been created yet.'),
  ];
  $build['pager'] = [
    '#theme' => 'pager',
    '#element' => 0,
    '#parameters' => [],
    '#route_name' => '<none>',
    '#tags' => [],
    '#quantity' => 9,
  ];
  return $build;
}