You are here

public function FormHelper::displayEntitySettings in Simple XML sitemap 8.3

Same name and namespace in other branches
  1. 8.2 src/Form/FormHelper.php \Drupal\simple_sitemap\Form\FormHelper::displayEntitySettings()
  2. 4.x src/Form/FormHelper.php \Drupal\simple_sitemap\Form\FormHelper::displayEntitySettings()

Parameters

$form_fragment:

Return value

$this

File

src/Form/FormHelper.php, line 281

Class

FormHelper
Class FormHelper @package Drupal\simple_sitemap\Form

Namespace

Drupal\simple_sitemap\Form

Code

public function displayEntitySettings(&$form_fragment) {
  $bundle_name = !empty($this
    ->getBundleName()) ? $this->entityHelper
    ->getBundleLabel($this
    ->getEntityTypeId(), $this
    ->getBundleName()) : $this
    ->t('undefined');
  $variants = $this->generator
    ->getSitemapManager()
    ->getSitemapVariants(NULL, FALSE);
  $form_fragment['settings']['#markup'] = empty($variants) ? $this
    ->t('At least one sitemap variants needs to be defined for a bundle to be indexable.<br>Variants can be configured <a href="@url">here</a>.', [
    '@url' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap/variants',
  ]) : '<strong>' . $this
    ->t('Sitemap variants') . '</strong>';
  foreach ($variants as $variant => $definition) {
    $form_fragment['settings'][$variant] = [
      '#type' => 'details',
      '#title' => '<em>' . $this
        ->t($definition['label']) . '</em>',
      '#open' => !empty($this->settings[$variant]['index']),
    ];

    // Disable fields of entity instance whose bundle is not indexed.
    $form_fragment['settings'][$variant]['#disabled'] = $this
      ->getEntityCategory() === 'instance' && empty($this->settings[$variant]['bundle_settings']['index']);

    // Index
    $form_fragment['settings'][$variant]['index_' . $variant . '_' . $this
      ->getEntityTypeId() . '_settings'] = [
      '#type' => 'radios',
      '#default_value' => (int) $this->settings[$variant]['index'],
      '#options' => [
        $this
          ->getEntityCategory() === 'instance' ? $this
          ->t('Do not index this <em>@bundle</em> entity in variant <em>@variant_label</em>', [
          '@bundle' => $bundle_name,
          '@variant_label' => $this
            ->t($variants[$variant]['label']),
        ]) : $this
          ->t('Do not index entities of type <em>@bundle</em> in variant <em>@variant_label</em>', [
          '@bundle' => $bundle_name,
          '@variant_label' => $this
            ->t($variants[$variant]['label']),
        ]),
        $this
          ->getEntityCategory() === 'instance' ? $this
          ->t('Index this <em>@bundle entity</em> in variant <em>@variant_label</em>', [
          '@bundle' => $bundle_name,
          '@variant_label' => $this
            ->t($variants[$variant]['label']),
        ]) : $this
          ->t('Index entities of type <em>@bundle</em> in variant <em>@variant_label</em>', [
          '@bundle' => $bundle_name,
          '@variant_label' => $this
            ->t($variants[$variant]['label']),
        ]),
      ],
      '#attributes' => [
        'class' => [
          'enabled-for-variant',
          $variant,
        ],
      ],
    ];
    if ($this
      ->getEntityCategory() === 'instance' && isset($this->settings[$variant]['bundle_settings']['index'])) {
      $form_fragment['settings'][$variant]['index_' . $variant . '_' . $this
        ->getEntityTypeId() . '_settings']['#options'][(int) $this->settings[$variant]['bundle_settings']['index']] .= ' <em>(' . $this
        ->t('default') . ')</em>';
    }

    // Priority
    $form_fragment['settings'][$variant]['priority_' . $variant . '_' . $this
      ->getEntityTypeId() . '_settings'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Priority'),
      '#description' => $this
        ->getEntityCategory() === 'instance' ? $this
        ->t('The priority this <em>@bundle</em> entity will have in the eyes of search engine bots.', [
        '@bundle' => $bundle_name,
      ]) : $this
        ->t('The priority entities of this type will have in the eyes of search engine bots.'),
      '#default_value' => $this->settings[$variant]['priority'],
      '#options' => $this
        ->getPrioritySelectValues(),
      '#states' => [
        'visible' => [
          ':input[name="index_' . $variant . '_' . $this
            ->getEntityTypeId() . '_settings"]' => [
            'value' => 1,
          ],
        ],
      ],
    ];
    if ($this
      ->getEntityCategory() === 'instance' && isset($this->settings[$variant]['bundle_settings']['priority'])) {
      $form_fragment['settings'][$variant]['priority_' . $variant . '_' . $this
        ->getEntityTypeId() . '_settings']['#options'][$this
        ->formatPriority($this->settings[$variant]['bundle_settings']['priority'])] .= ' (' . $this
        ->t('default') . ')';
    }

    // Changefreq
    $form_fragment['settings'][$variant]['changefreq_' . $variant . '_' . $this
      ->getEntityTypeId() . '_settings'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Change frequency'),
      '#description' => $this
        ->getEntityCategory() === 'instance' ? $this
        ->t('The frequency with which this <em>@bundle</em> entity changes. Search engine bots may take this as an indication of how often to index it.', [
        '@bundle' => $bundle_name,
      ]) : $this
        ->t('The frequency with which entities of this type change. Search engine bots may take this as an indication of how often to index them.'),
      '#default_value' => isset($this->settings[$variant]['changefreq']) ? $this->settings[$variant]['changefreq'] : NULL,
      '#options' => $this
        ->getChangefreqSelectValues(),
      '#states' => [
        'visible' => [
          ':input[name="index_' . $variant . '_' . $this
            ->getEntityTypeId() . '_settings"]' => [
            'value' => 1,
          ],
        ],
      ],
    ];
    if ($this
      ->getEntityCategory() === 'instance' && isset($this->settings[$variant]['bundle_settings']['changefreq'])) {
      $form_fragment['settings'][$variant]['changefreq_' . $variant . '_' . $this
        ->getEntityTypeId() . '_settings']['#options'][$this->settings[$variant]['bundle_settings']['changefreq']] .= ' (' . $this
        ->t('default') . ')';
    }

    // Images
    $form_fragment['settings'][$variant]['include_images_' . $variant . '_' . $this
      ->getEntityTypeId() . '_settings'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Include images'),
      '#description' => $this
        ->getEntityCategory() === 'instance' ? $this
        ->t('Determines if images referenced by this <em>@bundle</em> entity should be included in the sitemap.', [
        '@bundle' => $bundle_name,
      ]) : $this
        ->t('Determines if images referenced by entities of this type should be included in the sitemap.'),
      '#default_value' => isset($this->settings[$variant]['include_images']) ? (int) $this->settings[$variant]['include_images'] : 0,
      '#options' => [
        $this
          ->t('No'),
        $this
          ->t('Yes'),
      ],
      '#states' => [
        'visible' => [
          ':input[name="index_' . $variant . '_' . $this
            ->getEntityTypeId() . '_settings"]' => [
            'value' => 1,
          ],
        ],
      ],
    ];
    if ($this
      ->getEntityCategory() === 'instance' && isset($this->settings[$variant]['bundle_settings']['include_images'])) {
      $form_fragment['settings'][$variant]['include_images_' . $variant . '_' . $this
        ->getEntityTypeId() . '_settings']['#options'][(int) $this->settings[$variant]['bundle_settings']['include_images']] .= ' (' . $this
        ->t('default') . ')';
    }
  }
  return $this;
}