Commit 1b9ef2dd authored by gbyte.co's avatar gbyte.co

Add possibility to disable settings for certain entity types

parent 3442174e
...@@ -3,10 +3,15 @@ custom: ...@@ -3,10 +3,15 @@ custom:
path: / path: /
priority: 1.0 priority: 1.0
entity_types: [] entity_types:
node: []
taxonomy_term: []
user: []
menu_link_content: []
settings: settings:
max_links: 2000 max_links: 2000
cron_generate: true cron_generate: true
remove_duplicates: true remove_duplicates: true
batch_process_limit: 1500 batch_process_limit: 1500
...@@ -82,5 +82,28 @@ function simple_sitemap_install() { ...@@ -82,5 +82,28 @@ function simple_sitemap_install() {
$sitemap = \Drupal::service('simple_sitemap.generator'); $sitemap = \Drupal::service('simple_sitemap.generator');
$sitemap->generateSitemap('nobatch'); $sitemap->generateSitemap('nobatch');
$base_url = $GLOBALS['base_url']; $base_url = $GLOBALS['base_url'];
drupal_set_message(t("You can now include content into the <a href='@sitemap_url' target='_blank'>XML sitemap</a> by visiting the corresponding entity type edit pages (e.g. <a href='@content_type_url' target='_blank'>content type</a>, <a href='@taxonomy_vocabulary_url' target='_blank'>taxonomy vocabulary</a>, <a href='@menu_url' target='_blank'>menu</a> and <a href='@user_url' target='_blank'>user</a> pages.)<br/>Custom links can be added on <a href='@config_url' target='_blank'>this configuration page</a>.", array('@sitemap_url' => "$base_url/sitemap.xml", '@content_type_url' => "$base_url/admin/structure/types", '@taxonomy_vocabulary_url' => "$base_url/admin/structure/taxonomy", '@menu_url' => "$base_url/admin/structure/menu", '@user_url' => "$base_url/admin/config/people/accounts", '@config_url' => "$base_url/admin/config/search/simplesitemap/custom"))); drupal_set_message(t("You can now include content into the <a href='@sitemap_url' target='_blank'>XML sitemap</a> by visiting the corresponding entity type edit pages (e.g. <a href='@content_type_url' target='_blank'>node type edit pages</a>) These settings can then be overridden for single entities.<br/> Support for additional entity types and custom links can be added on <a href='@config_url' target='_blank'>the module's configuration pages</a>.", array('@sitemap_url' => "$base_url/sitemap.xml", '@content_type_url' => "$base_url/admin/structure/types", '@config_url' => "$base_url/admin/config/search/simplesitemap")));
}
/**
* Changing the data structure of the module's configuration.
*/
function simple_sitemap_update_8201() {
$entity_types = \Drupal::config('simple_sitemap.settings')->get('entity_types');
$entity_types = is_array($entity_types) ? $entity_types : array();
$naming_changes = [
'node_type' => 'node',
'taxonomy_vocabulary' => 'taxonomy_term',
'menu' => 'menu_link_content',
'commerce_product_type' => 'commerce_product',
'media_bundle' => 'media',
];
foreach($entity_types as $entity_type_name => $settings) {
if (isset($naming_changes[$entity_type_name])) {
$entity_types[$naming_changes[$entity_type_name]] = $entity_types[$entity_type_name];
unset($entity_types[$entity_type_name]);
}
}
\Drupal::service('config.factory')->getEditable('simple_sitemap.settings')
->set('entity_types', $entity_types)->save();
} }
...@@ -4,6 +4,11 @@ simple_sitemap.settings: ...@@ -4,6 +4,11 @@ simple_sitemap.settings:
base_route: simple_sitemap.settings base_route: simple_sitemap.settings
weight: -1 weight: -1
simple_sitemap.settings_entities:
route_name: simple_sitemap.settings_entities
title: 'Entities'
base_route: simple_sitemap.settings
simple_sitemap.settings_custom: simple_sitemap.settings_custom:
route_name: simple_sitemap.settings_custom route_name: simple_sitemap.settings_custom
title: 'Custom links' title: 'Custom links'
......
...@@ -36,7 +36,11 @@ function simple_sitemap_form_alter(&$form, $form_state, $form_id) { ...@@ -36,7 +36,11 @@ function simple_sitemap_form_alter(&$form, $form_state, $form_id) {
// Get current entity type sitemap settings. // Get current entity type sitemap settings.
$entity_types = $sitemap->getConfig('entity_types'); $entity_types = $sitemap->getConfig('entity_types');
// Do not alter the form, if sitemap is disabled for entity type. // Do not alter the form if entity is not enabled in sitemap settings.
if (!isset($entity_types[$f->entityTypeId]))
return;
// Do not alter the form, if sitemap is disabled for the entity type of this entity instance.
if ($f->entityCategory == 'instance' && empty($entity_types[$f->entityTypeId][$f->bundleName]['index'])) if ($f->entityCategory == 'instance' && empty($entity_types[$f->entityTypeId][$f->bundleName]['index']))
return; return;
...@@ -50,7 +54,7 @@ function simple_sitemap_form_alter(&$form, $form_state, $form_id) { ...@@ -50,7 +54,7 @@ function simple_sitemap_form_alter(&$form, $form_state, $form_id) {
$bundle_priority = $priority = $entity_types[$f->entityTypeId][$f->bundleName]['priority']; $bundle_priority = $priority = $entity_types[$f->entityTypeId][$f->bundleName]['priority'];
} }
// Overwriting defaults if settings found for entity. // Overwriting defaults if settings found for this entity instance.
if ($f->entityCategory == 'instance') { if ($f->entityCategory == 'instance') {
if (isset($entity_types[$f->entityTypeId][$f->bundleName]['entities'][$f->instanceId]['index'])) { if (isset($entity_types[$f->entityTypeId][$f->bundleName]['entities'][$f->instanceId]['index'])) {
$index = $entity_types[$f->entityTypeId][$f->bundleName]['entities'][$f->instanceId]['index']; $index = $entity_types[$f->entityTypeId][$f->bundleName]['entities'][$f->instanceId]['index'];
...@@ -189,19 +193,20 @@ function simple_sitemap_cron() { ...@@ -189,19 +193,20 @@ function simple_sitemap_cron() {
* Implements hook_entity_bundle_delete(). * Implements hook_entity_bundle_delete().
* *
* Removes settings of the removed bundle. * Removes settings of the removed bundle.
*
* @todo Not working for menu bundles, as they are technically not bundles.
*/ */
function simple_sitemap_entity_bundle_delete($entity_type_id, $bundle) { function simple_sitemap_entity_bundle_delete($entity_type_id, $bundle) {
$sitemap = \Drupal::service('simple_sitemap.generator'); $sitemap = \Drupal::service('simple_sitemap.generator');
$entity_types = $sitemap->getConfig('entity_types'); $entity_types = $sitemap->getConfig('entity_types');
$bundle_entity_type_id = \Drupal::entityTypeManager()->getDefinition($entity_type_id)->getBundleEntityType(); if (isset($entity_types[$entity_type_id][$bundle])) {
if (isset($entity_types[$bundle_entity_type_id][$bundle])) { unset($entity_types[$entity_type_id][$bundle]);
unset($entity_types[$bundle_entity_type_id][$bundle]);
$sitemap->saveConfig('entity_types', $entity_types); $sitemap->saveConfig('entity_types', $entity_types);
$message = t("You may want to <a href='@url'>regenerate</a> your XML sitemap now.", array('@url' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap')); $message = t("You may want to <a href='@url'>regenerate</a> your XML sitemap now.", array('@url' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap'));
if ($sitemap->getSetting('cron_generate')) { if ($sitemap->getSetting('cron_generate')) {
$message .= '</br>' . t('Otherwise the sitemap will be regenerated on the next cron run.'); $message .= '</br>' . t('Otherwise the sitemap will be regenerated on the next cron run.');
} }
drupal_set_message($message); // drupal_set_message($message); // Commented out, as html code is visible.
} }
} }
...@@ -213,13 +218,11 @@ function simple_sitemap_entity_bundle_delete($entity_type_id, $bundle) { ...@@ -213,13 +218,11 @@ function simple_sitemap_entity_bundle_delete($entity_type_id, $bundle) {
function simple_sitemap_entity_delete(Drupal\Core\Entity\EntityInterface $entity) { function simple_sitemap_entity_delete(Drupal\Core\Entity\EntityInterface $entity) {
$sitemap = \Drupal::service('simple_sitemap.generator'); $sitemap = \Drupal::service('simple_sitemap.generator');
$entity_types = $sitemap->getConfig('entity_types'); $entity_types = $sitemap->getConfig('entity_types');
$entity_type = $entity->getEntityType(); $entity_type_id = $entity->getEntityTypeId();
if (!empty($entity_type->getBundleEntityType())) { $bundle_name = !empty($entity->bundle()) ? $entity->bundle() : $entity_type_id;
$bundle_entity_type = $entity_type->getBundleEntityType(); $bundle_name = $bundle_name == 'menu_link_content'&& method_exists($entity, 'getMenuName') ? $entity->getMenuName() : $bundle_name; // Menu fix.
$bundle = $entity->bundle(); if (isset($entity_types[$entity_type_id][$bundle_name]['entities'][$entity->id()])) {
if (isset($entity_types[$bundle_entity_type][$bundle]['entities'][$entity->id()])) { unset($entity_types[$entity_type_id][$bundle_name]['entities'][$entity->id()]);
unset($entity_types[$bundle_entity_type][$bundle]['entities'][$entity->id()]); $sitemap->saveConfig('entity_types', $entity_types);
$sitemap->saveConfig('entity_types', $entity_types);
}
} }
} }
...@@ -20,6 +20,14 @@ simple_sitemap.settings: ...@@ -20,6 +20,14 @@ simple_sitemap.settings:
requirements: requirements:
_permission: 'administer sitemap settings' _permission: 'administer sitemap settings'
simple_sitemap.settings_entities:
path: '/admin/config/search/simplesitemap/entities'
defaults:
_form: '\Drupal\simple_sitemap\Form\SimplesitemapEntitiesForm'
_title: 'Simple XML Sitemap Settings'
requirements:
_permission: 'administer sitemap settings'
simple_sitemap.settings_custom: simple_sitemap.settings_custom:
path: '/admin/config/search/simplesitemap/custom' path: '/admin/config/search/simplesitemap/custom'
defaults: defaults:
......
...@@ -171,7 +171,8 @@ class Batch { ...@@ -171,7 +171,8 @@ class Batch {
$results = $query->execute(); $results = $query->execute();
if (!empty($results)) { if (!empty($results)) {
$entities = entity_load_multiple($entity_info['entity_type_name'], $results); // $entities = entity_load_multiple($entity_info['entity_type_name'], $results);
$entities = \Drupal::entityTypeManager()->getStorage($entity_info['entity_type_name'])->loadMultiple($results);
foreach ($entities as $entity_id => $entity) { foreach ($entities as $entity_id => $entity) {
if (self::isBatch($batch_info)) { if (self::isBatch($batch_info)) {
...@@ -179,20 +180,14 @@ class Batch { ...@@ -179,20 +180,14 @@ class Batch {
} }
// Overriding entity settings if it has been overridden on entity edit page... // Overriding entity settings if it has been overridden on entity edit page...
$bundle_name = !empty($entity_info['bundle_name']) ? $entity_info['bundle_name'] : NULL; if (isset($batch_info['entity_types'][$entity_info['entity_type_name']][$entity_info['bundle_name']]['entities'][$entity_id]['index'])) {
$bundle_entity_type = !empty($entity_info['bundle_entity_type']) ? $entity_info['bundle_entity_type'] : NULL;
$bundle_entity_type = $bundle_entity_type == 'menu_link_content' ? 'menu' : $bundle_entity_type; // Menu fix
if (!empty($bundle_name) && !empty($bundle_entity_type)
&& isset($batch_info['entity_types'][$bundle_entity_type][$bundle_name]['entities'][$entity_id]['index'])) {
// Skipping entity if it has been excluded on entity edit page. // Skipping entity if it has been excluded on entity edit page.
if (!$batch_info['entity_types'][$bundle_entity_type][$bundle_name]['entities'][$entity_id]['index']) { if (!$batch_info['entity_types'][$entity_info['entity_type_name']][$entity_info['bundle_name']]['entities'][$entity_id]['index']) {
continue; continue;
} }
// Otherwise overriding priority settings for this entity. // Otherwise overriding priority settings for this entity.
$priority = $batch_info['entity_types'][$bundle_entity_type][$bundle_name]['entities'][$entity_id]['priority']; $priority = $batch_info['entity_types'][$entity_info['entity_type_name']][$entity_info['bundle_name']]['entities'][$entity_id]['priority'];
} }
// Loading url object for menu links. // Loading url object for menu links.
...@@ -225,8 +220,10 @@ class Batch { ...@@ -225,8 +220,10 @@ class Batch {
$urls[$default_language_id] = $url_object->toString(); $urls[$default_language_id] = $url_object->toString();
} }
else { else {
// if ($entity->hasTranslation($language->getId())) {
$url_object->setOption('language', $language); $url_object->setOption('language', $language);
$urls[$language->getId()] = $url_object->toString(); $urls[$language->getId()] = $url_object->toString();
// }
} }
} }
...@@ -310,7 +307,7 @@ class Batch { ...@@ -310,7 +307,7 @@ class Batch {
self::processSegment($context, $batch_info); self::processSegment($context, $batch_info);
} }
private static function pathProcessed($path, &$context) { //todo: test functionality private static function pathProcessed($path, &$context) {
$path_pool = isset($context['results']['processed_paths']) ? $context['results']['processed_paths'] : array(); $path_pool = isset($context['results']['processed_paths']) ? $context['results']['processed_paths'] : array();
if (in_array($path, $path_pool)) { if (in_array($path, $path_pool)) {
return TRUE; return TRUE;
......
...@@ -68,10 +68,8 @@ class Form { ...@@ -68,10 +68,8 @@ class Form {
private function getEntityDataFromFormEntity() { private function getEntityDataFromFormEntity() {
$form_entity = $this->getFormEntity(); $form_entity = $this->getFormEntity();
if ($form_entity !== FALSE) { if ($form_entity !== FALSE) {
$form_entity_type = $form_entity->getEntityType(); $entity_type_id = $form_entity->getEntityTypeId();
$entity_type_id = $form_entity->getEntityTypeId(); //todo: Change to $form_entity_type->id()?
$sitemap_entity_types = Simplesitemap::getSitemapEntityTypes(); $sitemap_entity_types = Simplesitemap::getSitemapEntityTypes();
$bundle_entity_type = $form_entity_type->getBundleEntityType();
$entity_bundle = $form_entity->bundle(); $entity_bundle = $form_entity->bundle();
if (isset($sitemap_entity_types[$entity_type_id])) { if (isset($sitemap_entity_types[$entity_type_id])) {
$this->entityCategory = 'instance'; $this->entityCategory = 'instance';
...@@ -85,24 +83,22 @@ class Form { ...@@ -85,24 +83,22 @@ class Form {
} }
} }
// Menu fixes. // Menu fix.
if (is_null($this->entityCategory) && $entity_type_id == 'menu') { if (is_null($this->entityCategory) && $entity_type_id == 'menu') {
$this->entityCategory = 'bundle'; $this->entityCategory = 'bundle';
} $entity_type_id = 'menu_link_content';
if ($entity_type_id == 'menu_link_content') {
$bundle_entity_type = 'menu';
} }
switch ($this->entityCategory) { switch ($this->entityCategory) {
case 'bundle': case 'bundle':
$this->entityTypeId = $form_entity->getEntityTypeId(); $this->entityTypeId = $entity_type_id == 'menu_link_content' ? $entity_type_id : $form_entity->getEntityType()->getBundleOf(); // Menu fix.
$this->bundleName = $form_entity->id(); $this->bundleName = $form_entity->id();
$this->instanceId = NULL; $this->instanceId = NULL;
break; break;
case 'instance': case 'instance':
$this->entityTypeId = !empty($bundle_entity_type) ? $bundle_entity_type : $entity_bundle; $this->entityTypeId = $entity_type_id;
$this->bundleName = $entity_bundle == 'menu_link_content' && method_exists($form_entity, 'getMenuName') ? $form_entity->getMenuName() : $entity_bundle; // menu fix $this->bundleName = $entity_bundle == 'menu_link_content' && method_exists($form_entity, 'getMenuName') ? $form_entity->getMenuName() : $entity_bundle; // Menu fix.
$this->instanceId = $form_entity->id(); $this->instanceId = $form_entity->id();
break; break;
...@@ -124,7 +120,7 @@ class Form { ...@@ -124,7 +120,7 @@ class Form {
$form_object = $this->formState->getFormObject(); $form_object = $this->formState->getFormObject();
if (!is_null($form_object) if (!is_null($form_object)
&& method_exists($form_object, 'getEntity') && method_exists($form_object, 'getEntity')
&& $form_object->getOperation() !== 'delete') { && in_array($form_object->getOperation(), ['default', 'edit'])) {
return $form_object->getEntity(); return $form_object->getEntity();
} }
return FALSE; return FALSE;
......
<?php
/**
* @file
* Contains \Drupal\simple_sitemap\Form\SimplesitemapEntitiesForm.
*/
namespace Drupal\simple_sitemap\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\simple_sitemap\Simplesitemap;
/**
* SimplesitemapSettingsFrom
*/
class SimplesitemapEntitiesForm extends ConfigFormBase {
/**
* {@inheritdoc}
*/
public function getFormID() {
return 'simple_sitemap_entities_form';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return ['simple_sitemap.settings'];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$sitemap = \Drupal::service('simple_sitemap.generator');
$form['simple_sitemap_entities']['entities'] = array(
'#title' => t('Supported entities'),
'#type' => 'fieldset',
'#markup' => '<p>' . t('XML sitemap settings will be added only to entity forms of entities enabled here. Disabling an entity on this page will irreversibly delete its sitemap settings.') . '</p>',
);
$options = [];
$entity_types = Simplesitemap::getSitemapEntityTypes();
foreach ($entity_types as $entity_type_id => $entity_type) {
$options[$entity_type_id] = $entity_type->getLabel() ? : $entity_type_id;
}
$form['simple_sitemap_entities']['entities']['entities'] = array(
'#type' => 'checkboxes',
'#title' => t('Enable sitemap settings'),
'#description' => t(''),
'#options' => $options,
'#default_value' => array_keys($sitemap->getConfig('entity_types')),
);
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$sitemap = \Drupal::service('simple_sitemap.generator');
$entity_types = $sitemap->getConfig('entity_types');
foreach($form_state->getValue('entities') as $entity_type_name => $enable) {
if (!$enable) {
unset($entity_types[$entity_type_name]);
}
elseif (empty($entity_types[$entity_type_name])) {
$entity_types[$entity_type_name] = [];
}
}
$sitemap->saveConfig('entity_types', $entity_types);
parent::submitForm($form, $form_state);
}
}
...@@ -34,7 +34,7 @@ class Simplesitemap { ...@@ -34,7 +34,7 @@ class Simplesitemap {
* Gets a specific sitemap configuration from the configuration storage. * Gets a specific sitemap configuration from the configuration storage.
* *
* @param string $key * @param string $key
* Configuration key, like 'entity_links'. * Configuration key, like 'entity_types'.
* @return mixed * @return mixed
* The requested configuration. * The requested configuration.
*/ */
...@@ -46,7 +46,7 @@ class Simplesitemap { ...@@ -46,7 +46,7 @@ class Simplesitemap {
* Saves a specific sitemap configuration to db. * Saves a specific sitemap configuration to db.
* *
* @param string $key * @param string $key
* Configuration key, like 'entity_links'. * Configuration key, like 'entity_types'.
* @param mixed $value * @param mixed $value
* The configuration to be saved. * The configuration to be saved.
*/ */
......
...@@ -59,38 +59,27 @@ class SitemapGenerator { ...@@ -59,38 +59,27 @@ class SitemapGenerator {
} }
/** /**
* Collects the entity path generating information from all simeple_sitemap * Collects entity metadata for entities that are set to be indexed
* plugins to be added to the batch. * and returns a batch-ready operation.
* *
* @return array $operations. * @return array $operations.
*/ */
private function batchAddEntityTypePaths() { private function batchAddEntityTypePaths() {
$operations = []; $operations = [];
$sitemap_entity_types = Simplesitemap::getSitemapEntityTypes();
// Menu fix. foreach($this->entityTypes as $entity_type_name => $bundles) {
if (isset($this->entityTypes['menu'])) { if (isset($sitemap_entity_types[$entity_type_name])) {
$this->entityTypes['menu_link_content'] = $this->entityTypes['menu']; $keys = $sitemap_entity_types[$entity_type_name]->getKeys();
unset ($this->entityTypes['menu']); $keys['bundle'] = $entity_type_name == 'menu_link_content' ? 'menu_name' : $keys['bundle']; // Menu fix.
} foreach($bundles as $bundle_name => $bundle_settings) {
$operations[] = [
$entity_types = Simplesitemap::getSitemapEntityTypes(); 'entity_info' => [
foreach($entity_types as $entity_type_name => $entity_type) { 'bundle_settings' => $bundle_settings,
$bundle_entity_type_name = !empty($entity_type->getBundleEntityType()) ? $entity_type->getBundleEntityType() : $entity_type->id(); 'bundle_name' => $bundle_name,
if (isset($this->entityTypes[$bundle_entity_type_name])) { 'entity_type_name' => $entity_type_name,
foreach($this->entityTypes[$bundle_entity_type_name] as $bundle_name => $bundle_settings) { 'keys' => $keys,
if ($bundle_settings['index']) { ],
$keys = $entity_type->getKeys(); ];
if ($entity_type_name == 'menu_link_content') {$keys['bundle'] = 'menu_name';} // Menu fix.
$operations[] = [
'entity_info' => [
'bundle_settings' => $bundle_settings,
'bundle_name' => $bundle_name,
'bundle_entity_type' => $bundle_entity_type_name,
'entity_type_name' => $entity_type_name,
'keys' => $keys,
],
];
}
} }
} }
} }
......
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