Commit 134f4ee0 authored by gbyte.co's avatar gbyte.co Committed by gbyte.co

Issue #2684863 by Michèle: Integrating in entity edit forms to override bundle settings.

parent 600a7db0
......@@ -42,3 +42,14 @@ simple_sitemap.settings:
priority:
label: 'Priority'
type: float
entities:
label: 'Entities'
type: sequence
sequence:
type: mapping
mapping:
label: 'Index'
type: integer
priority:
label: 'Priority'
type: float
name: Simple XML Sitemap
type: module
description: 'This module creates a standard conform xml sitemap of your content.'
description: 'Creates a standard conform XML sitemap of your content.'
configure: simple_sitemap.settings
package: SEO
core: 8.x
......@@ -26,16 +26,39 @@ function simple_sitemap_form_alter(&$form, $form_state, $form_id) {
return;
$entity_type_id = $sitemap_form_entity_data['entity_type_id'];
$bundle_name = $sitemap_form_entity_data['bundle_name'];
$entity_id = $sitemap_form_entity_data['entity_id'];
$sitemap = \Drupal::service('simple_sitemap.generator');
// Get current entity type sitemap settings.
$entity_types = $sitemap->getConfig('entity_types');
// Do not add sitemap settings to entity form, if sitemap is disabled for
// entity type.
if (!empty($entity_id) && empty($entity_types[$entity_type_id][$bundle_name]['index']))
return;
// Setting default form values.
$index_content_checked = FALSE;
$priority = SitemapGenerator::PRIORITY_DEFAULT;
// Overwriting defaults if settings found for bundle.
if (isset($entity_types[$entity_type_id][$bundle_name]['index'])) {
$index_content_checked = $entity_types[$entity_type_id][$bundle_name]['index'];
$priority = $entity_types[$entity_type_id][$bundle_name]['priority'];
}
// Overwriting defaults if settings found for entity.
if (isset($entity_types[$entity_type_id][$bundle_name]['entities'][$entity_id]['index'])) {
$index_content_checked = $entity_types[$entity_type_id][$bundle_name]['entities'][$entity_id]['index'];
$priority = $entity_types[$entity_type_id][$bundle_name]['entities'][$entity_id]['priority'];
}
$form['simple_sitemap'] = array(
'#type' => 'details',
'#group' => 'additional_settings',
'#group' => isset($form['additional_settings']) ? 'additional_settings' : 'advanced',
'#title' => t('Simple XML sitemap'),
'#description' => !empty($entity_id) ? t('Settings for this specific entity can be overridden here.') : '',
);
// Attach some js magic to forms.
......@@ -45,18 +68,16 @@ function simple_sitemap_form_alter(&$form, $form_state, $form_id) {
$form['simple_sitemap']['#attached']['library'][] = 'simple_sitemap/fieldsetSummaries';
}
$index_content_checked = isset($entity_types[$entity_type_id][$bundle_name]['index']) ? $entity_types[$entity_type_id][$bundle_name]['index'] : FALSE;
$form['simple_sitemap']['simple_sitemap_index_content'] = array(
'#type' => 'checkbox',
'#title' => t('Index content of this type'),
'#title' => !empty($entity_id) ? t('Index this entity') : 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['simple_sitemap']['simple_sitemap_priority'] = array(
'#type' => 'select',
'#title' => t('Priority'),
'#description' => t('The priority entities of this bundle will have in the eyes of search engine bots.'),
'#description' => !empty($entity_id) ? t('The priority this entity will have in the eyes of search engine bots.') : t('The priority entities of this bundle will have in the eyes of search engine bots.'),
'#options' => SitemapGenerator::getPrioritySelectValues(),
'#default_value' => $priority,
);
......@@ -73,10 +94,15 @@ function simple_sitemap_form_alter(&$form, $form_state, $form_id) {
$form['#simple_sitemap']['entity_type_id'] = $entity_type_id;
$form['#simple_sitemap']['bundle_name'] = $bundle_name;
$form['#simple_sitemap']['entity_id'] = $entity_id;
// Add submission handler.
if (isset($form['actions']['submit']['#submit'])) {
$form['actions']['submit']['#submit'][] = 'simple_sitemap_entity_form_submit';
foreach (array_keys($form['actions']) as $action) {
if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
$form['actions'][$action]['#submit'][] = 'simple_sitemap_entity_form_submit';
}
}
}
else { // Fix for account page rendering other submit handlers not usable.
$form['#submit'][] = 'simple_sitemap_entity_form_submit';
......@@ -87,7 +113,6 @@ function simple_sitemap_form_alter(&$form, $form_state, $form_id) {
* Form submission handler called in hook_form_alter.
*/
function simple_sitemap_entity_form_submit($form, &$form_state) {
$sitemap = \Drupal::service('simple_sitemap.generator');
$values = $form_state->getValues();
......@@ -97,13 +122,28 @@ function simple_sitemap_entity_form_submit($form, &$form_state) {
$entity_type_id = $form['#simple_sitemap']['entity_type_id'];
$bundle_name = $form['#simple_sitemap']['bundle_name'];
$entity_id = $form['#simple_sitemap']['entity_id'];
// Get current entity type sitemap settings.
$entity_types = $sitemap->getConfig('entity_types');
$entity_types[$entity_type_id][$bundle_name]['index'] = $values['simple_sitemap_index_content'];
$entity_types[$entity_type_id][$bundle_name]['priority'] = $values['simple_sitemap_priority'];
if (!empty($entity_id)) {
// Delete overrides if they are identical to bundle settings.
if ($values['simple_sitemap_index_content'] == $entity_types[$entity_type_id][$bundle_name]['index']
&& $values['simple_sitemap_priority'] == $entity_types[$entity_type_id][$bundle_name]['priority']) {
unset($entity_types[$entity_type_id][$bundle_name]['entities'][$entity_id]);
}
// Else save overrides for this entity.
else {
$entity_types[$entity_type_id][$bundle_name]['entities'][$entity_id]['index'] = $values['simple_sitemap_index_content'];
$entity_types[$entity_type_id][$bundle_name]['entities'][$entity_id]['priority'] = $values['simple_sitemap_priority'];
}
}
// Save bundle settings.
else {
$entity_types[$entity_type_id][$bundle_name]['index'] = $values['simple_sitemap_index_content'];
$entity_types[$entity_type_id][$bundle_name]['priority'] = $values['simple_sitemap_priority'];
}
// Save new entity type settings.
$sitemap->saveConfig('entity_types', $entity_types);
}
......@@ -134,7 +174,6 @@ function simple_sitemap_entity_bundle_delete($entity_type_id, $bundle) {
$sitemap = \Drupal::service('simple_sitemap.generator');
$entity_types = $sitemap->getConfig('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->saveConfig('entity_types', $entity_types);
......@@ -145,3 +184,22 @@ function simple_sitemap_entity_bundle_delete($entity_type_id, $bundle) {
drupal_set_message($message);
}
}
/**
* Implements hook_entity_delete().
*
* Removes settings of the removed entity.
*/
function simple_sitemap_entity_delete(Drupal\Core\Entity\EntityInterface $entity) {
$sitemap = \Drupal::service('simple_sitemap.generator');
$entity_types = $sitemap->getConfig('entity_types');
$entity_type = $entity->getEntityType();
if (!empty($entity_type->getBundleEntityType())) {
$bundle_entity_type = $entity_type->getBundleEntityType();
$bundle = $entity->bundle();
if (isset($entity_types[$bundle_entity_type][$bundle]['entities'][$entity->id()])) {
unset($entity_types[$bundle_entity_type][$bundle]['entities'][$entity->id()]);
$sitemap->saveConfig('entity_types', $entity_types);
}
}
}
......@@ -39,6 +39,7 @@ class Batch {
'batch_process_limit' => $config['batch_process_limit'],
'max_links' => $config['max_links'],
'remove_duplicates' => $config['remove_duplicates'],
'entity_types' => \Drupal::config('simple_sitemap.settings')->get('entity_types'),
'anonymous_user_account' => User::load(self::ANONYMOUS_USER_ID),
);
}
......@@ -104,6 +105,7 @@ class Batch {
array('@url' => $GLOBALS['base_url'] . '/sitemap.xml')));
}
else {
//todo: register error
}
}
......@@ -146,9 +148,7 @@ class Batch {
}
// Creating a query limited to n=batch_process_limit entries.
$query
->condition($id_field, $context['sandbox']['current_id'], '>')
->orderBy($id_field);
$query->condition($id_field, $context['sandbox']['current_id'], '>')->orderBy($id_field);
if (!empty($batch_info['batch_process_limit']))
$query->range(0, $batch_info['batch_process_limit']);
$result = $query->execute()->fetchAll();
......@@ -156,6 +156,19 @@ class Batch {
foreach ($result as $row) {
self::SetCurrentId($row->$id_field, $context);
// Overriding entity settings if it has been overridden on entity edit page...
$bundle_name = !empty($info['bundle_settings']['bundle_name']) ? $info['bundle_settings']['bundle_name'] : NULL;
$bundle_entity_type = !empty($info['bundle_settings']['bundle_entity_type']) ? $info['bundle_settings']['bundle_entity_type'] : NULL;
if (!empty($bundle_name) && !empty($bundle_entity_type)
&& isset($batch_info['entity_types'][$bundle_entity_type][$bundle_name]['entities'][$row->$id_field]['index'])) {
// Skipping entity if it has been excluded on entity edit page.
if (!$batch_info['entity_types'][$bundle_entity_type][$bundle_name]['entities'][$row->$id_field]['index']) {
continue;
}
// Otherwise overriding priority settings for this entity.
$priority = $batch_info['entity_types'][$bundle_entity_type][$bundle_name]['entities'][$row->$id_field]['priority'];
}
// Setting route parameters if they exist in the database (menu links).
if (isset($route_params_field) && !empty($route_parameters = unserialize($row->$route_params_field))) {
$route_parameters = array(key($route_parameters) => $route_parameters[key($route_parameters)]);
......@@ -213,8 +226,9 @@ class Batch {
'urls' => $urls,
'options' => $url_object->getOptions(),
'lastmod' => !empty($info['field_info']['lastmod']) ? date_iso8601($row->{$info['field_info']['lastmod']}) : NULL,
'priority' => !empty($info['bundle_settings']['priority']) ? $info['bundle_settings']['priority'] : NULL,
'priority' => !empty($priority) ? $priority : (!empty($info['bundle_settings']['priority']) ? $info['bundle_settings']['priority'] : NULL),
);
$priority = NULL;
}
self::setProgressInfo($context, $batch_info);
self::processSegment($context, $batch_info);
......
......@@ -45,5 +45,4 @@ class NodeType extends LinkGeneratorBase {
->condition('type', $bundle)
->condition('status', 1);
}
}
......@@ -25,7 +25,7 @@ class User extends LinkGeneratorBase {
* {@inheritdoc}
*/
public function getInfo() {
array(
return array(
'field_info' => array(
'entity_id' => 'uid',
'lastmod' => 'changed',
......
......@@ -55,27 +55,48 @@ class Simplesitemap {
return array(
'entity_type_id' => $plugin['id'],
'bundle_name' => $plugin['id'],
'entity_id' => NULL,
);
}
}
// Else get entity type id and bundle name from the form if available and only
// if a simple_sitemap plugin of the same entity type exists.
$form_entity = self::getFormEntity($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 = $form_entity->getEntityType();
// If this entity is of a bundle, this will be an entity add/edit page.
// If a simple_sitemap plugin of this entity_type exists, return the
// entity type ID, the bundle name and ethe entity ID.
if (!empty($entity_type->getBundleEntityType())) {
$bundle_entity_type = $entity_type->getBundleEntityType();
if (isset($plugins[$bundle_entity_type])) {
return array(
'entity_type_id' => $form_entity_type_id,
'bundle_name' => $form_entity->Id(),
'entity_type_id' => $bundle_entity_type,
'bundle_name' => $form_entity->bundle(),
'entity_id' => $form_entity->Id(),
);
}
}
// Else if this entity has an entity type ID, it means it is a bundle
// configuration form. If a simple_sitemap plugin of this entity_type
// exists, return the entity type ID, the bundle name and ethe entity ID.
else {
$entity_type_id = $form_entity->getEntityTypeId();
if (isset($plugins[$entity_type_id])) {
if (!isset($plugins[$entity_type_id]['form_id'])
|| $plugins[$entity_type_id]['form_id'] === $form_id) {
return array(
'entity_type_id' => $entity_type_id,
'bundle_name' => $form_entity->Id(),
'entity_id' => NULL,
);
}
}
}
}
// If both methods of getting simple_sitemap entity data for this form
// If all methods of getting simple_sitemap entity data for this form
// failed, return FALSE.
return FALSE;
}
......
......@@ -96,6 +96,8 @@ class SitemapGenerator {
$operation['info'] = $instance->getInfo();
$operation['query'] = $instance->getQuery($bundle);
$operation['info']['bundle_settings'] = $bundle_settings;
$operation['info']['bundle_settings']['bundle_name'] = $bundle;
$operation['info']['bundle_settings']['bundle_entity_type'] = $link_generator_plugin['id'];
$operations[] = $operation;
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment