You are here

public function MetatagManager::form in Metatag 8

Builds the form element for a Metatag field.

If a list of either groups or tags are passed in, those will be used to limit the groups/tags on the form. If nothing is passed in, all groups and tags will be used.

Parameters

array $values: Existing values.

array $element: Existing element.

array $token_types: Token types to return in the tree.

array $included_groups: Available group plugins.

array $included_tags: Available tag plugins.

bool $verbose_help: Whether to include extra help text at the top of the form or keep it short.

Return value

array Render array for metatag form.

Overrides MetatagManagerInterface::form

File

src/MetatagManager.php, line 260

Class

MetatagManager
Class MetatagManager.

Namespace

Drupal\metatag

Code

public function form(array $values, array $element, array $token_types = [], array $included_groups = NULL, array $included_tags = NULL, $verbose_help = FALSE) {

  // Add the outer fieldset.
  $element += [
    '#type' => 'details',
  ];

  // Add a title to the form.
  $element['preamble'] = [
    '#markup' => '<p><strong>' . $this
      ->t('Configure the meta tags below.') . '</strong></p>',
    '#weight' => -11,
  ];
  $element += $this->tokenService
    ->tokenBrowser($token_types, $verbose_help);
  $groups_and_tags = $this
    ->sortedGroupsWithTags();
  foreach ($groups_and_tags as $group_name => $group) {

    // Only act on groups that have tags and are in the list of included
    // groups (unless that list is null).
    if (isset($group['tags']) && (is_null($included_groups) || in_array($group_name, $included_groups) || in_array($group['id'], $included_groups))) {

      // Create the fieldset.
      $element[$group_name]['#type'] = 'details';
      $element[$group_name]['#title'] = $group['label'];
      $element[$group_name]['#description'] = $group['description'];
      $element[$group_name]['#open'] = FALSE;
      foreach ($group['tags'] as $tag_name => $tag) {

        // Only act on tags in the included tags list, unless that is null.
        if (is_null($included_tags) || in_array($tag_name, $included_tags) || in_array($tag['id'], $included_tags)) {

          // Make an instance of the tag.
          $tag = $this->tagPluginManager
            ->createInstance($tag_name);

          // Set the value to the stored value, if any.
          $tag_value = isset($values[$tag_name]) ? $values[$tag_name] : NULL;
          $tag
            ->setValue($tag_value);

          // Open any groups that have non-empty values.
          if (!empty($tag_value)) {
            $element[$group_name]['#open'] = TRUE;
          }

          // Create the bit of form for this tag.
          $element[$group_name][$tag_name] = $tag
            ->form($element);
        }
      }
    }
  }
  return $element;
}