getDefinitions();
// Go through simplesitemap plugins and check if one of them declares usage
// of this particular form. If that's the case, get entity type id of the
// plugin definition and assume the bundle to be of the same name as the
// entity type id.
foreach($plugins as $plugin) {
if (isset($plugin['form_id']) && $plugin['form_id'] === $form_id) {
$entity_type_id = $plugin['id'];
$bundle_name = $entity_type_id;
}
}
// Else get entity type id and bundle name from the form if available and only
// if a simplesitemap plugin of the same entity type exists.
if (empty($entity_type_id)) {
$form_entity = Simplesitemap::get_form_entity($form_state);
if ($form_entity !== FALSE) {
$form_entity_type_id = $form_entity->getEntityTypeId();
if (isset($plugins[$form_entity_type_id])) {
if (!isset($plugins[$form_entity_type_id]['form_id'])
|| $plugins[$form_entity_type_id]['form_id'] === $form_id) {
$entity_type_id = $form_entity_type_id;
$bundle_name = $form_entity->Id();
}
}
}
}
// If both methods of getting simplesitemap configuration for this form
// failed, return.
if (empty($entity_type_id))
return;
$sitemap = new Simplesitemap;
// Get current entity type sitemap settings.
$entity_types = $sitemap->get_entity_types();
$form['simplesitemap'] = array(
'#group' => 'additional_settings',
'#title' => t('Simple XML sitemap'),
'#type' => 'details'
);
$index_content_checked = isset($entity_types[$entity_type_id][$bundle_name]['index']) ? $entity_types[$entity_type_id][$bundle_name]['index'] : FALSE;
$form['simplesitemap']['simplesitemap_index_content'] = array(
'#type' => 'checkbox',
'#title' => t('Index content of this type'),
'#default_value' => $index_content_checked,
);
$priority = isset($entity_types[$entity_type_id][$bundle_name]['priority']) ? $entity_types[$entity_type_id][$bundle_name]['priority'] : SitemapGenerator::PRIORITY_DEFAULT;
$form['simplesitemap']['simplesitemap_priority'] = array(
'#type' => 'select',
'#title' => t('Priority'),
'#description' => t('The priority entities of this bundle will have in the eyes of search engine bots.'),
'#options' => SitemapGenerator::get_priority_select_values(),
'#default_value' => $priority,
);
$form['simplesitemap']['simplesitemap_rebuild_now'] = array(
'#type' => 'checkbox',
'#title' => t('Rebuild sitemap after hitting Save'),
'#description' => t('This setting will rebuild the whole sitemap including the above changes.
Otherwise the sitemap will be rebuilt on next cron run.'),
'#default_value' => FALSE,
);
$form['#simplesitemap']['entity_type_id'] = $entity_type_id;
$form['#simplesitemap']['bundle_name'] = $bundle_name;
// Add submission handler.
$form['actions']['submit']['#submit'][] = 'simplesitemap_entity_form_submit';
}
/**
* Form submission handler called in hook_form_alter.
*/
function simplesitemap_entity_form_submit($form, &$form_state) {
$sitemap = new Simplesitemap;
$values = $form_state->getValues();
// Only make changes in DB if sitemap settings actually changed.
if ($values['simplesitemap_index_content'] != $form['simplesitemap']['simplesitemap_index_content']['#default_value']
|| $values['simplesitemap_priority'] != $form['simplesitemap']['simplesitemap_priority']['#default_value']) {
$entity_type_id = $form['#simplesitemap']['entity_type_id'];
$bundle_name = $form['#simplesitemap']['bundle_name'];
// Get current entity type sitemap settings.
$entity_types = $sitemap->get_entity_types();
$entity_types[$entity_type_id][$bundle_name]['index'] = $values['simplesitemap_index_content'];
$entity_types[$entity_type_id][$bundle_name]['priority'] = $values['simplesitemap_priority'];
// Save new entity type settings.
$sitemap->save_entity_types($entity_types);
}
// Regenerate sitemaps according to user setting.
if ($values['simplesitemap_rebuild_now']) {
$sitemap->generate_sitemap();
}
}
/**
* Implements hook_cron.
*/
function simplesitemap_cron() {
// Regenerate sitemap for all languages.
$sitemap = new Simplesitemap;
$sitemap->generate_sitemap();
}
/**
* Implements hook_entity_bundle_delete().
*
* Removes settings of the removed bundle.
*/
function simplesitemap_entity_bundle_delete($entity_type_id, $bundle) {
$sitemap = new Simplesitemap;
$entity_types = $sitemap->get_entity_types();
$bundle_entity_type_id = \Drupal::entityManager()->getDefinition($entity_type_id)->getBundleEntityType();
if (isset($entity_types[$bundle_entity_type_id][$bundle])) {
unset($entity_types[$bundle_entity_type_id][$bundle]);
$sitemap->save_entity_types($entity_types);
drupal_set_message(t("You may want to rebuild your XML sitemap now.
Otherwise it will be rebuilt on the next cron run.",
array('@url' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap')));
}
}