Commit dadcd579 authored by gbyte.co's avatar gbyte.co

Various improvements regarding bundle instance sitemap settings overrides.

parent 42a9c468
......@@ -10,7 +10,7 @@
attach: function(context) {
$(context).find('#edit-simple-sitemap').drupalSetSummary(function (context) {
var vals = [];
if ($(context).find('#edit-simple-sitemap-index-content').is(':checked')) {
if ($(context).find('#edit-simple-sitemap-index-content-1').is(':checked')) {
// Display summary of the settings in tabs.
vals.push(Drupal.t('Included in sitemap'));
......
......@@ -13,7 +13,7 @@
Drupal.behaviors.simple_sitemapForm = {
attach: function(context) {
if ($(context).find('#edit-simple-sitemap-index-content').is(':checked')) {
if ($(context).find('#edit-simple-sitemap-index-content-1').is(':checked')) {
// Show 'Priority' field if 'Index sitemap' is ticked.
$('.form-item-simple-sitemap-priority').show();
}
......@@ -24,7 +24,7 @@
// Show 'Regenerate sitemap' field if setting has changed.
$( "#edit-simple-sitemap-index-content" ).change(function() {
$('.form-item-simple-sitemap-regenerate-now').show();
if ($(context).find('#edit-simple-sitemap-index-content').is(':checked')) {
if ($(context).find('#edit-simple-sitemap-index-content-1').is(':checked')) {
// Show 'Priority' field if 'Index sitemap' is ticked.
$('.form-item-simple-sitemap-priority').show();
}
......
......@@ -4,8 +4,8 @@
* Main module file containing hooks.
*/
use Drupal\simple_sitemap\Simplesitemap;
use Drupal\simple_sitemap\SitemapGenerator;
use Drupal\simple_sitemap\Form;
/**
* Implements hook_help.
......@@ -21,12 +21,11 @@ function simple_sitemap_help($route_name, \Drupal\Core\Routing\RouteMatchInterfa
* Adds sitemap settings to entity types that are supported via plugins.
*/
function simple_sitemap_form_alter(&$form, $form_state, $form_id) {
$sitemap_form_entity_data = Simplesitemap::getSitemapFormEntityData($form_state, $form_id);
if (empty($sitemap_form_entity_data))
$f = new Form($form, $form_state, $form_id);
if (empty($f->entityType)) {
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');
......@@ -35,30 +34,32 @@ function simple_sitemap_form_alter(&$form, $form_state, $form_id) {
// 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']))
if ($f->entityType == 'bundle_instance' && empty($entity_types[$f->entityTypeId][$f->bundleName]['index']))
return;
// Setting default form values.
$index_content_checked = FALSE;
$index = 0;
$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'];
if (isset($entity_types[$f->entityTypeId][$f->bundleName]['index'])) {
$bundle_index = $index = $entity_types[$f->entityTypeId][$f->bundleName]['index'];
$bundle_priority = $priority = $entity_types[$f->entityTypeId][$f->bundleName]['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'];
if ($f->entityType == 'bundle_instance') {
if (isset($entity_types[$f->entityTypeId][$f->bundleName]['entities'][$f->entityId]['index'])) {
$index = $entity_types[$f->entityTypeId][$f->bundleName]['entities'][$f->entityId]['index'];
$priority = $entity_types[$f->entityTypeId][$f->bundleName]['entities'][$f->entityId]['priority'];
}
}
$form['simple_sitemap'] = array(
'#type' => 'details',
'#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.') : '',
'#description' => $f->entityType == 'bundle_instance' ? t('Settings for this specific entity can be overridden here.') : '',
);
// Attach some js magic to forms.
......@@ -69,18 +70,27 @@ function simple_sitemap_form_alter(&$form, $form_state, $form_id) {
}
$form['simple_sitemap']['simple_sitemap_index_content'] = array(
'#type' => 'checkbox',
'#title' => !empty($entity_id) ? t('Index this entity') : t('Index content of this type'),
'#default_value' => $index_content_checked,
'#type' => 'radios',
'#default_value' => $index,
'#options' => [
0 => $f->entityType == 'bundle_instance' ? t('Do not index this entity') : t('Do not index content of this type'),
1 => $f->entityType == 'bundle_instance' ? t('Index this entity') : t('Index content of this type'),
]
);
if ($f->entityType == 'bundle_instance' && isset($bundle_index)) {
$form['simple_sitemap']['simple_sitemap_index_content']['#options'][$bundle_index] .= ' <em>(' . t('Default') . ')</em>';
}
$form['simple_sitemap']['simple_sitemap_priority'] = array(
'#type' => 'select',
'#title' => t('Priority'),
'#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(),
'#description' => $f->entityType == 'bundle_instance' ? 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.'),
'#default_value' => $priority,
'#options' => SitemapGenerator::getPrioritySelectValues(),
);
if ($f->entityType == 'bundle_instance' && isset($bundle_priority)) {
$form['simple_sitemap']['simple_sitemap_priority']['#options'][(string)$bundle_priority] .= ' (' . t('Default') . ')';
}
$form['simple_sitemap']['simple_sitemap_regenerate_now'] = array(
'#type' => 'checkbox',
......@@ -92,9 +102,7 @@ function simple_sitemap_form_alter(&$form, $form_state, $form_id) {
$form['simple_sitemap']['simple_sitemap_regenerate_now']['#description'] .= '</br>' . t('Otherwise the sitemap will be rebuilt on next cron run.');
}
$form['#simple_sitemap']['entity_type_id'] = $entity_type_id;
$form['#simple_sitemap']['bundle_name'] = $bundle_name;
$form['#simple_sitemap']['entity_id'] = $entity_id;
$form['#simple_sitemap'] = $f;
// Add submission handler.
if (isset($form['actions']['submit']['#submit'])) {
......@@ -113,44 +121,49 @@ 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();
// Only make changes in DB if sitemap settings actually changed.
if ($values['simple_sitemap_index_content'] != $form['simple_sitemap']['simple_sitemap_index_content']['#default_value']
|| $values['simple_sitemap_priority'] != $form['simple_sitemap']['simple_sitemap_priority']['#default_value']) {
if (Form::valuesChanged($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'];
$sitemap = \Drupal::service('simple_sitemap.generator');
$f = $form['#simple_sitemap'];
$values = $form_state->getValues();
// Get current entity type sitemap settings.
$entity_types = $sitemap->getConfig('entity_types');
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'];
switch ($f->entityType) {
case 'custom':
case 'bundle':
$f->bundleName = !empty($f->bundleName) ? $f->bundleName : Form::getNewEntityId($form_state);
$entity_types[$f->entityTypeId][$f->bundleName]['index'] = $values['simple_sitemap_index_content'];
$entity_types[$f->entityTypeId][$f->bundleName]['priority'] = $values['simple_sitemap_priority'];
break;
case 'bundle_instance':
$f->entityId = !empty($f->entityId) ? $f->entityId : Form::getNewEntityId($form_state);
// Delete overrides if they are identical to bundle settings.
if ($values['simple_sitemap_index_content'] == $entity_types[$f->entityTypeId][$f->bundleName]['index']
&& $values['simple_sitemap_priority'] == $entity_types[$f->entityTypeId][$f->bundleName]['priority']) {
unset($entity_types[$f->entityTypeId][$f->bundleName]['entities'][$f->entityId]);
}
// Else save overrides for this entity.
else {
$entity_types[$f->entityTypeId][$f->bundleName]['entities'][$f->entityId]['index'] = $values['simple_sitemap_index_content'];
$entity_types[$f->entityTypeId][$f->bundleName]['entities'][$f->entityId]['priority'] = $values['simple_sitemap_priority'];
}
break;
}
// Save new entity type settings.
$sitemap->saveConfig('entity_types', $entity_types);
}
// Regenerate sitemaps according to user setting.
if ($values['simple_sitemap_regenerate_now']) {
$sitemap->generateSitemap();
// Regenerate sitemaps according to user setting.
if ($values['simple_sitemap_regenerate_now']) {
$sitemap->generateSitemap(); //todo: generates changes?
}
}
}
......@@ -173,7 +186,7 @@ function simple_sitemap_cron() {
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();
$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]);
$sitemap->saveConfig('entity_types', $entity_types);
......
<?php
/**
* @file
* Contains \Drupal\simple_sitemap\Form.
*/
namespace Drupal\simple_sitemap;
/**
* Form class.
*/
class Form {
public $entityType;
public $entityTypeId;
public $bundleName;
public $entityId;
private $plugins;
private $formState;
private $formId;
/**
* Form constructor.
*/
function __construct(&$form, $form_state, $form_id) {
$this->formId = $form_id;
$this->formState = $form_state;
// Get all simple_sitemap plugins.
$manager = \Drupal::service('plugin.manager.simple_sitemap');
$this->plugins = $manager->getDefinitions();
// First look for a plugin declaring usage of this form, if this fails,
// check if this is a bundle, or bundle instance form and gather the form
// entity's sitemap settings from the database.
if (!$this->getEntityDataFromCustomPlugin()) {
$this->getEntityDataFromForm();
}
}
/**
* Checks if a plugin defines this form to be used for its entity settings and
* sets the entity settings and collects those settings.
*
* @return bool
* TRUE if there is a plugin declaring usage of this form, FALSE otherwise.
*/
private function getEntityDataFromCustomPlugin() {
foreach($this->plugins as $plugin) {
if (isset($plugin['form_id']) && $plugin['form_id'] === $this->formId) {
$this->entityType = 'custom';
$this->entityTypeId = $plugin['id'];
$this->bundleName = $plugin['id'];
return TRUE;
}
}
return FALSE;
}
/**
* Checks if this particular form is a bundle form, or a bundle instance form
* and gathers sitemap settings from the database.
*
* @return bool
* TRUE if this is a bundle or bundle instance form, FALSE otherwise.
*/
private function getEntityDataFromForm() {
$form_entity = $this->getFormEntity();
if ($form_entity !== FALSE) {
$entity_type = $form_entity->getEntityType();
if (!empty($entity_type->getBundleEntityType())) {
$this->entityType = 'bundle_instance';
$this->entityTypeId = $entity_type->getBundleEntityType();
$this->bundleName = $form_entity->bundle();
$this->entityId = $form_entity->id();
return TRUE;
}
else {
$entity_type_id = $form_entity->getEntityTypeId();
if (isset($this->plugins[$entity_type_id])) {
if (!isset($this->plugins[$entity_type_id]['form_id'])
|| $this->plugins[$entity_type_id]['form_id'] === $this->formId) {
$this->entityType = 'bundle';
$this->entityTypeId = $entity_type_id;
$this->bundleName = $form_entity->id();
return TRUE;
}
}
}
}
return FALSE;
}
/**
* Gets the object entity of the form if available.
*
* @return object $entity or FALSE if non-existent or if form operation is
* 'delete'.
*/
private function getFormEntity() {
$form_object = $this->formState->getFormObject();
if (!is_null($form_object)
&& method_exists($form_object, 'getEntity')
&& $form_object->getOperation() !== 'delete') {
return $form_object->getEntity();
}
return FALSE;
}
/**
* Gets new entity Id after entity creation.
* To be used in an entity form submit.
*
* @return int entity ID.
*/
public static function getNewEntityId($form_state) {
return $form_state->getFormObject()->getEntity()->id();
}
/**
* Checks if simple_sitemap values have been changed after submitting the form.
* To be used in an entity form submit.
*
* @return bool
* TRUE if simple_sitemap form values have been altered by the user.
*/
public static function valuesChanged($form, $form_state) {
$values = $form_state->getValues();
foreach (array('simple_sitemap_index_content', 'simple_sitemap_priority', 'simple_sitemap_regenerate_now') as $field_name) {
if ($values[$field_name] != $form['simple_sitemap'][$field_name]['#default_value']) {
return TRUE;
}
}
return FALSE;
}
}
......@@ -5,8 +5,8 @@
*/
namespace Drupal\simple_sitemap;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Cache\Cache;
/**
......@@ -30,96 +30,6 @@ class Simplesitemap {
$this->sitemap = db_query("SELECT * FROM {simple_sitemap}")->fetchAllAssoc('id');
}
/**
* Gets the entity_type_id and bundle_name of the form object if available and only
* if the sitemap supports this entity type through an existing plugin.
*
* @param object $form_state
* @param string $form_id
*
* @return array containing the entity_type_id and the bundle_name of the
* form object or FALSE if none found or not supported by an existing plugin.
*/
public static function getSitemapFormEntityData($form_state, $form_id) {
// Get all simple_sitemap plugins.
$manager = \Drupal::service('plugin.manager.simple_sitemap');
$plugins = $manager->getDefinitions();
// Go through simple_sitemap plugins and check if one of them declares usage
// of this particular form. If that's the case, get the 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) {
return array(
'entity_type_id' => $plugin['id'],
'bundle_name' => $plugin['id'],
'entity_id' => NULL,
);
}
}
$form_entity = self::getFormEntity($form_state);
if ($form_entity !== FALSE) {
$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' => $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 all methods of getting simple_sitemap entity data for this form
// failed, return FALSE.
return FALSE;
}
/**
* Gets the object entity of the form if available.
*
* @param object $form_state
*
* @return object $entity or FALSE if non-existent or if form operation is
* 'delete'.
*/
private static function getFormEntity($form_state) {
$form_object = $form_state->getFormObject();
if (!is_null($form_object)
&& method_exists($form_state->getFormObject(), 'getEntity')
&& $form_object->getOperation() !== 'delete') {
$entity = $form_state->getFormObject()->getEntity();
return $entity;
}
return FALSE;
}
/**
* Gets a specific sitemap configuration from the configuration storage.
*
......@@ -180,6 +90,10 @@ class Simplesitemap {
/**
* Generates the sitemap for all languages and saves it to the db.
*
* @param string $from
* Can be 'form', 'cron', or 'drush'. This decides how to the batch process
* is to be run.
*/
public function generateSitemap($from = 'form') {
Cache::invalidateTags(array('simple_sitemap'));
......
<?php
/**
* @file
* Contains \Drupal\simple_sitemap\Tests\SimplesitemapTest
......@@ -7,7 +6,6 @@
namespace Drupal\simple_sitemap\Tests;
use Drupal\simple_sitemap\Simplesitemap;
use Drupal\simpletest\WebTestBase;
/**
......
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