You are here

function xmlsitemap_add_form_link_options in XML sitemap 2.x

Same name and namespace in other branches
  1. 8 xmlsitemap.module \xmlsitemap_add_form_link_options()
  2. 6.2 xmlsitemap.admin.inc \xmlsitemap_add_form_link_options()
  3. 7.2 xmlsitemap.admin.inc \xmlsitemap_add_form_link_options()

Add a link's XML sitemap options to the link's form.

@todo Add changefreq overridability.

Parameters

array $form: Form array.

string $entity_type: Entity type id.

string $bundle: Bundle id.

int $id: Entity id.

1 call to xmlsitemap_add_form_link_options()
xmlsitemap_form_alter in ./xmlsitemap.module
Implements hook_form_alter().

File

./xmlsitemap.module, line 1152
xmlsitemap XML sitemap

Code

function xmlsitemap_add_form_link_options(array &$form, $entity_type, $bundle, $id) {
  if (!($link = \Drupal::service('xmlsitemap.link_storage')
    ->load($entity_type, $id))) {
    $link = [];
  }
  $bundle_info = xmlsitemap_link_bundle_load($entity_type, $bundle);
  $link += [
    'access' => 1,
    'status' => $bundle_info['status'],
    'status_default' => $bundle_info['status'],
    'status_override' => 0,
    'priority' => $bundle_info['priority'],
    'priority_default' => $bundle_info['priority'],
    'priority_override' => 0,
    'changefreq' => $bundle_info['changefreq'],
  ];
  $currentUser = \Drupal::currentUser();
  $admin_permission = \Drupal::entityTypeManager()
    ->getDefinition($entity_type)
    ->getAdminPermission();
  $form['xmlsitemap'] = [
    '#type' => 'details',
    '#tree' => TRUE,
    '#title' => t('XML sitemap'),
    '#collapsible' => TRUE,
    '#collapsed' => !$link['status_override'] && !$link['priority_override'],
    '#access' => $currentUser
      ->hasPermission('administer xmlsitemap') || $admin_permission && $currentUser
      ->hasPermission($admin_permission),
    '#group' => 'advanced',
  ];

  // Show a warning if the link is not accessible and will not be included in
  // the sitemap.
  if ($id && !$link['access']) {
    $form['xmlsitemap']['warning'] = [
      '#type' => 'markup',
      '#prefix' => '<p><strong>',
      '#suffix' => '</strong></p>',
      '#value' => 'This item is not currently visible to anonymous users, so it will not be included in the sitemap.',
    ];
  }

  // Status field (inclusion/exclusion)
  $form['xmlsitemap']['status'] = [
    '#type' => 'select',
    '#title' => t('Inclusion'),
    '#options' => xmlsitemap_get_status_options($link['status_default']),
    '#default_value' => $link['status_override'] ? $link['status'] : 'default',
  ];
  $form['xmlsitemap']['status_default'] = [
    '#type' => 'value',
    '#value' => $link['status_default'],
  ];
  $form['xmlsitemap']['status_override'] = [
    '#type' => 'value',
    '#value' => $link['status_override'],
  ];

  // Priority field.
  $form['xmlsitemap']['priority'] = [
    '#type' => 'select',
    '#title' => t('Priority'),
    '#options' => xmlsitemap_get_priority_options($link['priority_default']),
    '#default_value' => $link['priority_override'] ? number_format($link['priority'], 1) : 'default',
    '#description' => t('The priority of this URL relative to other URLs on your site.'),
    '#states' => [
      'invisible' => [
        'select[name="xmlsitemap[status]"]' => [
          'value' => '0',
        ],
      ],
    ],
  ];
  $form['xmlsitemap']['changefreq'] = [
    '#type' => 'select',
    '#title' => t('Change frequency'),
    '#options' => xmlsitemap_get_changefreq_options(),
    '#default_value' => $link['changefreq'],
    '#description' => t('Select the frequency of changes.'),
    '#states' => [
      'invisible' => [
        'select[name="xmlsitemap[status]"]' => [
          'value' => '0',
        ],
      ],
    ],
  ];
  if (!$link['status_default']) {

    // If the default status is excluded, add a visible state on the include
    // override option.
    $form['xmlsitemap']['priority']['#states']['visible'] = [
      'select[name="xmlsitemap[status]"]' => [
        'value' => '1',
      ],
    ];
  }
  $form['xmlsitemap']['priority_default'] = [
    '#type' => 'value',
    '#value' => $link['priority_default'],
  ];
  $form['xmlsitemap']['priority_override'] = [
    '#type' => 'value',
    '#value' => $link['priority_override'],
  ];
  array_unshift($form['actions']['submit']['#submit'], 'xmlsitemap_process_form_link_options');
  if (!empty($form['actions']['publish']['#submit'])) {
    array_unshift($form['actions']['publish']['#submit'], 'xmlsitemap_process_form_link_options');
  }
}