You are here

protected function FillPdfFormForm::getEntityTypeOptions in FillPDF 8.4

Same name and namespace in other branches
  1. 5.0.x src/Form/FillPdfFormForm.php \Drupal\fillpdf\Form\FillPdfFormForm::getEntityTypeOptions()

Helper function providing available entity type select options.

After translating entity types to token types, we're filtering out those entity types that don't have any tokens supplied, because without tokens available there is no point populating a FillPDF form with an entity.

Return value

string[][] Multidimensional array of entity type options, keyed by entity type and grouped by the group label (i.e. 'Content' or 'Config').

1 call to FillPdfFormForm::getEntityTypeOptions()
FillPdfFormForm::form in src/Form/FillPdfFormForm.php
Gets the actual form array to be built.

File

src/Form/FillPdfFormForm.php, line 433

Class

FillPdfFormForm
Form controller for the FillPDFForm edit form.

Namespace

Drupal\fillpdf\Form

Code

protected function getEntityTypeOptions() {
  $token_info = $this->tokenResolver
    ->getTokenService()
    ->getInfo();
  $entity_mapper = $this->tokenResolver
    ->getEntityMapper();
  $all_tokens = $token_info['types'];
  $options = [];
  foreach ($this->entityTypeManager
    ->getDefinitions() as $entity_type => $definition) {

    // Exclude entity types with no tokens being provided.
    $token_type = $entity_mapper
      ->getTokenTypeForEntityType($entity_type, FALSE);
    if (!$token_type || empty($all_tokens[$token_type])) {
      continue;
    }
    $label = $definition
      ->getLabel();
    $group_label = (string) $definition
      ->getGroupLabel();
    $options[$group_label][$entity_type] = "{$entity_type} ({$label})";
  }
  foreach ($options as &$group_options) {

    // Sort each group's list alphabetically.
    array_multisort($group_options, SORT_ASC, SORT_NATURAL);
  }

  // Make sure that 'Content' entities are listed on top.
  $content = (string) $this
    ->t('Content', [], [
    'context' => 'Entity type group',
  ]);
  return [
    $content => $options[$content],
  ] + $options;
}