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:
path: /
priority: 1.0
entity_types: []
entity_types:
node: []
taxonomy_term: []
user: []
menu_link_content: []
settings:
max_links: 2000
cron_generate: true
remove_duplicates: true
batch_process_limit: 1500
......@@ -82,5 +82,28 @@ function simple_sitemap_install() {
$sitemap = \Drupal::service('simple_sitemap.generator');
$sitemap->generateSitemap('nobatch');
$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:
base_route: simple_sitemap.settings
weight: -1
simple_sitemap.settings_entities:
route_name: simple_sitemap.settings_entities
title: 'Entities'
base_route: simple_sitemap.settings
simple_sitemap.settings_custom:
route_name: simple_sitemap.settings_custom
title: 'Custom links'
......
......@@ -36,7 +36,11 @@ function simple_sitemap_form_alter(&$form, $form_state, $form_id) {
// Get current entity type sitemap settings.
$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']))
return;
......@@ -50,7 +54,7 @@ function simple_sitemap_form_alter(&$form, $form_state, $form_id) {
$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 (isset($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() {
* Implements hook_entity_bundle_delete().
*
* 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) {
$sitemap = \Drupal::service('simple_sitemap.generator');
$entity_types = $sitemap->getConfig('entity_types');
$bundle_entity_type_id = \Drupal::entityTypeManager()->getDefinition($entity_type_id)->getBundleEntityType();
if (isset($entity_types[$bundle_entity_type_id][$bundle])) {
unset($entity_types[$bundle_entity_type_id][$bundle]);
if (isset($entity_types[$entity_type_id][$bundle])) {
unset($entity_types[$entity_type_id][$bundle]);
$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'));
if ($sitemap->getSetting('cron_generate')) {
$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) {
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()]);
$entity_type_id = $entity->getEntityTypeId();
$bundle_name = !empty($entity->bundle()) ? $entity->bundle() : $entity_type_id;
$bundle_name = $bundle_name == 'menu_link_content'&& method_exists($entity, 'getMenuName') ? $entity->getMenuName() : $bundle_name; // Menu fix.
if (isset($entity_types[$entity_type_id][$bundle_name]['entities'][$entity->id()])) {
unset($entity_types[$entity_type_id][$bundle_name]['entities'][$entity->id()]);
$sitemap->saveConfig('entity_types', $entity_types);
}
}
}
......@@ -20,6 +20,14 @@ simple_sitemap.settings:
requirements:
_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:
path: '/admin/config/search/simplesitemap/custom'
defaults:
......
......@@ -171,7 +171,8 @@ class Batch {
$results = $query->execute();
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) {
if (self::isBatch($batch_info)) {
......@@ -179,20 +180,14 @@ class Batch {
}
// Overriding entity settings if it has been overridden on entity edit page...
$bundle_name = !empty($entity_info['bundle_name']) ? $entity_info['bundle_name'] : NULL;
$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'])) {
if (isset($batch_info['entity_types'][$entity_info['entity_type_name']][$entity_info['bundle_name']]['entities'][$entity_id]['index'])) {
// 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;
}
// 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.
......@@ -225,8 +220,10 @@ class Batch {
$urls[$default_language_id] = $url_object->toString();
}
else {
// if ($entity->hasTranslation($language->getId())) {
$url_object->setOption('language', $language);
$urls[$language->getId()] = $url_object->toString();
// }
}
}
......@@ -310,7 +307,7 @@ class Batch {
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();
if (in_array($path, $path_pool)) {
return TRUE;
......
......@@ -68,10 +68,8 @@ class Form {
private function getEntityDataFromFormEntity() {
$form_entity = $this->getFormEntity();
if ($form_entity !== FALSE) {
$form_entity_type = $form_entity->getEntityType();
$entity_type_id = $form_entity->getEntityTypeId(); //todo: Change to $form_entity_type->id()?
$entity_type_id = $form_entity->getEntityTypeId();
$sitemap_entity_types = Simplesitemap::getSitemapEntityTypes();
$bundle_entity_type = $form_entity_type->getBundleEntityType();
$entity_bundle = $form_entity->bundle();
if (isset($sitemap_entity_types[$entity_type_id])) {
$this->entityCategory = 'instance';
......@@ -85,24 +83,22 @@ class Form {
}
}
// Menu fixes.
// Menu fix.
if (is_null($this->entityCategory) && $entity_type_id == 'menu') {
$this->entityCategory = 'bundle';
}
if ($entity_type_id == 'menu_link_content') {
$bundle_entity_type = 'menu';
$entity_type_id = 'menu_link_content';
}
switch ($this->entityCategory) {
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->instanceId = NULL;
break;
case 'instance':
$this->entityTypeId = !empty($bundle_entity_type) ? $bundle_entity_type : $entity_bundle;
$this->bundleName = $entity_bundle == 'menu_link_content' && method_exists($form_entity, 'getMenuName') ? $form_entity->getMenuName() : $entity_bundle; // menu fix
$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->instanceId = $form_entity->id();
break;
......@@ -124,7 +120,7 @@ class Form {
$form_object = $this->formState->getFormObject();
if (!is_null($form_object)
&& method_exists($form_object, 'getEntity')
&& $form_object->getOperation() !== 'delete') {
&& in_array($form_object->getOperation(), ['default', 'edit'])) {
return $form_object->getEntity();
}
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 {
* Gets a specific sitemap configuration from the configuration storage.
*
* @param string $key
* Configuration key, like 'entity_links'.
* Configuration key, like 'entity_types'.
* @return mixed
* The requested configuration.
*/
......@@ -46,7 +46,7 @@ class Simplesitemap {
* Saves a specific sitemap configuration to db.
*
* @param string $key
* Configuration key, like 'entity_links'.
* Configuration key, like 'entity_types'.
* @param mixed $value
* The configuration to be saved.
*/
......
......@@ -59,33 +59,23 @@ class SitemapGenerator {
}
/**
* Collects the entity path generating information from all simeple_sitemap
* plugins to be added to the batch.
* Collects entity metadata for entities that are set to be indexed
* and returns a batch-ready operation.
*
* @return array $operations.
*/
private function batchAddEntityTypePaths() {
$operations = [];
// Menu fix.
if (isset($this->entityTypes['menu'])) {
$this->entityTypes['menu_link_content'] = $this->entityTypes['menu'];
unset ($this->entityTypes['menu']);
}
$entity_types = Simplesitemap::getSitemapEntityTypes();
foreach($entity_types as $entity_type_name => $entity_type) {
$bundle_entity_type_name = !empty($entity_type->getBundleEntityType()) ? $entity_type->getBundleEntityType() : $entity_type->id();
if (isset($this->entityTypes[$bundle_entity_type_name])) {
foreach($this->entityTypes[$bundle_entity_type_name] as $bundle_name => $bundle_settings) {
if ($bundle_settings['index']) {
$keys = $entity_type->getKeys();
if ($entity_type_name == 'menu_link_content') {$keys['bundle'] = 'menu_name';} // Menu fix.
$sitemap_entity_types = Simplesitemap::getSitemapEntityTypes();
foreach($this->entityTypes as $entity_type_name => $bundles) {
if (isset($sitemap_entity_types[$entity_type_name])) {
$keys = $sitemap_entity_types[$entity_type_name]->getKeys();
$keys['bundle'] = $entity_type_name == 'menu_link_content' ? 'menu_name' : $keys['bundle']; // Menu fix.
foreach($bundles as $bundle_name => $bundle_settings) {
$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,
],
......@@ -93,7 +83,6 @@ class SitemapGenerator {
}
}
}
}
return $operations;
}
......
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