You are here

public function XmlSitemapEntitiesSettingsForm::buildForm in XML sitemap 8

Same name and namespace in other branches
  1. 2.x src/Form/XmlSitemapEntitiesSettingsForm.php \Drupal\xmlsitemap\Form\XmlSitemapEntitiesSettingsForm::buildForm()

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 ConfigFormBase::buildForm

File

src/Form/XmlSitemapEntitiesSettingsForm.php, line 90

Class

XmlSitemapEntitiesSettingsForm
Configure what entities will be included in sitemap.

Namespace

Drupal\xmlsitemap\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildForm($form, $form_state);

  // Create the list of possible entity types.

  /** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */
  $entity_types = array_filter($this->entityTypeManager
    ->getDefinitions(), 'xmlsitemap_is_entity_type_supported');

  // Create the list of options as well as the default values based on which
  // entity types have enabled configuration already.
  $labels = array_map(function (EntityTypeInterface $entityType) {
    return $entityType
      ->getLabel();
  }, $entity_types);
  asort($labels);
  $defaults = array_keys(array_filter(array_map(function (EntityTypeInterface $entityType) {
    return xmlsitemap_link_entity_check_enabled($entityType
      ->id());
  }, $entity_types)));
  $form['entity_types'] = [
    '#title' => $this
      ->t('Custom sitemap entities settings'),
    '#type' => 'checkboxes',
    '#options' => $labels,
    '#default_value' => $defaults,
  ];
  $form['settings'] = [
    '#tree' => TRUE,
  ];
  foreach ($labels as $entity_type_id => $label) {
    $entity_type = $entity_types[$entity_type_id];
    $bundle_label = $entity_type
      ->getBundleLabel() ?: $label;
    $bundles = $this->entityTypeBundleInfo
      ->getBundleInfo($entity_type_id);
    $form['settings'][$entity_type_id] = [
      '#type' => 'container',
      '#entity_type' => $entity_type_id,
      '#bundle_label' => $bundle_label,
      '#title' => $bundle_label,
      '#states' => [
        'visible' => [
          ':input[name="entity_types[' . $entity_type_id . ']"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
      'types' => [
        '#type' => 'table',
        '#tableselect' => TRUE,
        '#default_value' => [],
        '#header' => [
          [
            'data' => $bundle_label,
            'class' => [
              'bundle',
            ],
          ],
          [
            'data' => $this
              ->t('Sitemap settings'),
            'class' => [
              'operations',
            ],
          ],
        ],
      ],
      '#access' => !empty($bundles),
    ];
    foreach ($bundles as $bundle => $bundle_info) {
      $form['settings'][$entity_type_id][$bundle]['settings'] = [
        '#type' => 'item',
        '#label' => $bundle_info['label'],
      ];
      $form['settings'][$entity_type_id]['types'][$bundle] = [
        'bundle' => [
          '#markup' => $bundle_info['label'],
        ],
        'operations' => [
          '#type' => 'operations',
          '#links' => [
            'configure' => [
              'title' => $this
                ->t('Configure'),
              'url' => Url::fromRoute('xmlsitemap.admin_settings_bundle', [
                'entity' => $entity_type_id,
                'bundle' => $bundle,
              ]),
              'query' => $this
                ->getDestinationArray(),
            ],
          ],
        ],
      ];
      $form['settings'][$entity_type_id]['types']['#default_value'][$bundle] = xmlsitemap_link_bundle_check_enabled($entity_type_id, $bundle);
    }
  }
  return $form;
}