Commit 2821811a authored by Pawel G's avatar Pawel G

Issue #2646768: Simple XML sitemap fieldset shows on node edit pages.

parent 27f49f79
...@@ -2,3 +2,5 @@ custom: ...@@ -2,3 +2,5 @@ custom:
- -
path: / path: /
priority: 1 priority: 1
entity_types: []
...@@ -39,7 +39,7 @@ function simplesitemap_install() { ...@@ -39,7 +39,7 @@ function simplesitemap_install() {
} }
/** /**
* Altering the {simplesitemap} language_code table field to hold longer strings. * Update: Altering the {simplesitemap} language_code table field to hold longer strings.
*/ */
function simplesitemap_update_8101() { function simplesitemap_update_8101() {
db_change_field('simplesitemap', 'language_code', 'language_code', array( db_change_field('simplesitemap', 'language_code', 'language_code', array(
...@@ -47,3 +47,20 @@ function simplesitemap_update_8101() { ...@@ -47,3 +47,20 @@ function simplesitemap_update_8101() {
'not null' => TRUE, 'not null' => TRUE,
'length' => 12,)); 'length' => 12,));
} }
/**
* Update: Changing config settings according to new config structure.
*/
function simplesitemap_update_8102() {
$entity_types = \Drupal::config('simplesitemap.settings')->get('entity_types');
$entity_types = is_array($entity_types) ? $entity_types : array();
if (isset($entity_types['node'])) {
$entity_types['node_type'] = $entity_types['node'];
unset($entity_types['node']);
}
if (isset($entity_types['taxonomy_term'])) {
$entity_types['taxonomy_vocabulary'] = $entity_types['taxonomy_term'];
unset($entity_types['taxonomy_term']);
}
\Drupal::service('config.factory')->getEditable('simplesitemap.settings')->set('entity_types', $entity_types)->save();
}
...@@ -13,49 +13,52 @@ use Drupal\simplesitemap\SitemapGenerator; ...@@ -13,49 +13,52 @@ use Drupal\simplesitemap\SitemapGenerator;
function simplesitemap_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) { function simplesitemap_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
$form_entity = Simplesitemap::get_form_entity($form_state); $form_entity = Simplesitemap::get_form_entity($form_state);
$entity_type_name = Simplesitemap::get_entity_type_name($form_entity); if (!$form_entity)
return;
if ($entity_type_name != FALSE && Simplesitemap::get_plugin_path($entity_type_name)) { $entity_type_id = $form_entity->getEntityTypeId();
if (!Simplesitemap::get_plugin_path($entity_type_id))
return;
$bundle_name = $form_entity->Id(); $bundle_name = $form_entity->Id();
$sitemap = new Simplesitemap; $sitemap = new Simplesitemap;
// Get current content type sitemap settings. // Get current entity type sitemap settings.
$entity_types = $sitemap->get_entity_types(); $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.<br/>Otherwise the sitemap will be rebuilt on next cron run.'),
'#default_value' => FALSE,
);
// Add submission handler.
$form['actions']['submit']['#submit'][] = 'simplesitemap_entity_form_submit';
$form['simplesitemap'] = array(
'#group' => 'additional_settings',
'#title' => t('Simple XML sitemap'),
'#type' => 'details'
);
$index_content_checked = isset($entity_types[$entity_type_name][$bundle_name]['index']) ? $entity_types[$entity_type_name][$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_name][$bundle_name]['priority']) ? $entity_types[$entity_type_name][$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.<br/>Otherwise the sitemap will be rebuilt on next cron run.'),
'#default_value' => FALSE,
);
// Add submission handler.
$form['actions']['submit']['#submit'][] = 'simplesitemap_entity_form_submit';
}
} }
/** /**
...@@ -70,16 +73,16 @@ function simplesitemap_entity_form_submit($form, &$form_state) { ...@@ -70,16 +73,16 @@ function simplesitemap_entity_form_submit($form, &$form_state) {
// Only make changes in DB if sitemap settings actually changed. // Only make changes in DB if sitemap settings actually changed.
if ($form['simplesitemap']['simplesitemap_index_content']['#default_value'] != $form['simplesitemap']['simplesitemap_index_content']['#value'] || $form['simplesitemap']['simplesitemap_priority']['#default_value'] != $form['simplesitemap']['simplesitemap_priority']['#value']) { if ($form['simplesitemap']['simplesitemap_index_content']['#default_value'] != $form['simplesitemap']['simplesitemap_index_content']['#value'] || $form['simplesitemap']['simplesitemap_priority']['#default_value'] != $form['simplesitemap']['simplesitemap_priority']['#value']) {
$entity = Simplesitemap::get_form_entity($form_state); $form_entity = Simplesitemap::get_form_entity($form_state);
$entity_type_name = Simplesitemap::get_entity_type_name($entity); $entity_type_id = $form_entity->getEntityTypeId();
$bundle_name = $entity->Id(); $bundle_name = $form_entity->Id();
// Get current entity type sitemap settings. // Get current entity type sitemap settings.
$entity_types = $sitemap->get_entity_types(); $entity_types = $sitemap->get_entity_types();
$values = $form_state->getValues(); $values = $form_state->getValues();
$entity_types[$entity_type_name][$bundle_name]['index'] = $values['simplesitemap_index_content']; $entity_types[$entity_type_id][$bundle_name]['index'] = $values['simplesitemap_index_content'];
$entity_types[$entity_type_name][$bundle_name]['priority'] = $values['simplesitemap_priority']; $entity_types[$entity_type_id][$bundle_name]['priority'] = $values['simplesitemap_priority'];
// Save new entity type settings. // Save new entity type settings.
$sitemap->save_entity_types($entity_types); $sitemap->save_entity_types($entity_types);
......
...@@ -23,8 +23,7 @@ abstract class EntityLinkGenerator { ...@@ -23,8 +23,7 @@ abstract class EntityLinkGenerator {
if (!$bundle_settings['index']) { if (!$bundle_settings['index']) {
continue; continue;
} }
$links = $this->get_entity_bundle_links($entity_type, $bundle, $language); $links = $this->get_entity_bundle_links($bundle, $language);
$lastmod = NULL;
foreach ($links as $id => $link) { foreach ($links as $id => $link) {
$this->entity_links[] = SitemapGenerator::add_xml_link_markup($link, $bundle_settings['priority'], $this->get_lastmod($entity_type, $id)); $this->entity_links[] = SitemapGenerator::add_xml_link_markup($link, $bundle_settings['priority'], $this->get_lastmod($entity_type, $id));
} }
...@@ -34,10 +33,10 @@ abstract class EntityLinkGenerator { ...@@ -34,10 +33,10 @@ abstract class EntityLinkGenerator {
private function get_lastmod($entity_type, $id) { private function get_lastmod($entity_type, $id) {
switch ($entity_type) { switch ($entity_type) {
case 'node': case 'node_type':
$lastmod = db_query("SELECT changed FROM {node_field_data} WHERE nid = :nid LIMIT 1", array(':nid' => $id))->fetchCol(); $lastmod = db_query("SELECT changed FROM {node_field_data} WHERE nid = :nid LIMIT 1", array(':nid' => $id))->fetchCol();
break; break;
case 'taxonomy_term': case 'taxonomy_vocabulary':
$lastmod = db_query("SELECT changed FROM {taxonomy_term_field_data} WHERE tid = :tid LIMIT 1", array(':tid' => $id))->fetchCol(); $lastmod = db_query("SELECT changed FROM {taxonomy_term_field_data} WHERE tid = :tid LIMIT 1", array(':tid' => $id))->fetchCol();
break; break;
case 'menu': case 'menu':
...@@ -46,5 +45,5 @@ abstract class EntityLinkGenerator { ...@@ -46,5 +45,5 @@ abstract class EntityLinkGenerator {
return isset($lastmod[0]) ? date_iso8601($lastmod[0]) : NULL; return isset($lastmod[0]) ? date_iso8601($lastmod[0]) : NULL;
} }
abstract function get_entity_bundle_links($entity_type, $bundle, $language); abstract function get_entity_bundle_links($bundle, $language);
} }
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
* Contains \Drupal\simplesitemap\LinkGenerators\EntityTypeLinkGenerators\menu. * Contains \Drupal\simplesitemap\LinkGenerators\EntityTypeLinkGenerators\menu.
* *
* Plugin for menu entity link generation. * Plugin for menu entity link generation.
* See \Drupal\simplesitemap\LinkGenerators\CustomLinkGenerator\node for more * See \Drupal\simplesitemap\LinkGenerators\CustomLinkGenerator\node_type for more
* documentation. * documentation.
*/ */
...@@ -18,7 +18,7 @@ use Drupal\Core\Url; ...@@ -18,7 +18,7 @@ use Drupal\Core\Url;
*/ */
class menu extends EntityLinkGenerator { class menu extends EntityLinkGenerator {
function get_entity_bundle_links($entity_type, $bundle, $language) { function get_entity_bundle_links($bundle, $language) {
$routes = db_query("SELECT mlid, route_name, route_parameters FROM {menu_tree} WHERE menu_name = :menu_name and enabled = 1", array(':menu_name' => $bundle)) $routes = db_query("SELECT mlid, route_name, route_parameters FROM {menu_tree} WHERE menu_name = :menu_name and enabled = 1", array(':menu_name' => $bundle))
->fetchAllAssoc('mlid'); ->fetchAllAssoc('mlid');
......
<?php <?php
/** /**
* @file * @file
* Contains \Drupal\simplesitemap\LinkGenerators\EntityTypeLinkGenerators\node. * Contains \Drupal\simplesitemap\LinkGenerators\EntityTypeLinkGenerators\node_type.
* *
* Plugin for node entity link generation. * Plugin for node entity link generation.
* *
* This can be used as a template to create new plugins for other entity types. * This can be used as a template to create new plugins for other configuration
* To create a plugin simply create a new class file in the * entity types. To create a plugin simply create a new class file in the
* EntityTypeLinkGenerators folder. Name this file after the entity type (eg. * EntityTypeLinkGenerators folder. Name this file after the configuration
* 'node' or 'taxonomy_term'. * entity type id (eg. 'node_type' or 'taxonomy_vocabulary'.)
* This class needs to extend the EntityLinkGenerator class and include * This class needs to extend the EntityLinkGenerator class and include
* the get_entity_bundle_links() method. - as shown here. This method has to * the get_entity_bundle_links() method. - as shown here. This method has to
* return an array of pure urls to the entities of the entity type in question. * return an array of pure urls to the entities of the configuration entity type
* in question.
*/ */
namespace Drupal\simplesitemap\LinkGenerators\EntityTypeLinkGenerators; namespace Drupal\simplesitemap\LinkGenerators\EntityTypeLinkGenerators;
...@@ -20,18 +21,18 @@ use Drupal\simplesitemap\LinkGenerators\EntityLinkGenerator; ...@@ -20,18 +21,18 @@ use Drupal\simplesitemap\LinkGenerators\EntityLinkGenerator;
use Drupal\Core\Url; use Drupal\Core\Url;
/** /**
* node class. * node_type class.
*/ */
class node extends EntityLinkGenerator { class node_type extends EntityLinkGenerator {
function get_entity_bundle_links($entity_type, $bundle, $language) { function get_entity_bundle_links($bundle, $language) {
$results = db_query("SELECT nid FROM {node_field_data} WHERE status = 1 AND type = :type", array(':type' => $bundle)) $results = db_query("SELECT nid FROM {node_field_data} WHERE status = 1 AND type = :type", array(':type' => $bundle))
->fetchAllAssoc('nid'); ->fetchAllAssoc('nid');
$urls = array(); $urls = array();
foreach ($results as $id => $changed) { foreach ($results as $id => $changed) {
$urls[$id] = Url::fromRoute("entity.$entity_type.canonical", array('node' => $id), array( $urls[$id] = Url::fromRoute("entity.node.canonical", array('node' => $id), array(
'language' => $language, 'language' => $language,
'absolute' => TRUE 'absolute' => TRUE
))->toString(); ))->toString();
......
<?php <?php
/** /**
* @file * @file
* Contains \Drupal\simplesitemap\LinkGenerators\EntityTypeLinkGenerators\taxonomy_term. * Contains \Drupal\simplesitemap\LinkGenerators\EntityTypeLinkGenerators\taxonomy_vocabulary.
* *
* Plugin for taxonomy term entity link generation. * Plugin for taxonomy term entity link generation.
* See \Drupal\simplesitemap\LinkGenerators\CustomLinkGenerator\node for more * See \Drupal\simplesitemap\LinkGenerators\CustomLinkGenerator\node_type for more
* documentation. * documentation.
*/ */
...@@ -14,18 +14,18 @@ use Drupal\simplesitemap\LinkGenerators\EntityLinkGenerator; ...@@ -14,18 +14,18 @@ use Drupal\simplesitemap\LinkGenerators\EntityLinkGenerator;
use Drupal\Core\Url; use Drupal\Core\Url;
/** /**
* taxonomy_term class. * taxonomy_vocabulary class.
*/ */
class taxonomy_term extends EntityLinkGenerator { class taxonomy_vocabulary extends EntityLinkGenerator {
function get_entity_bundle_links($entity_type, $bundle, $language) { function get_entity_bundle_links($bundle, $language) {
$results = db_query("SELECT tid FROM {taxonomy_term_field_data} WHERE vid = :vid", array(':vid' => $bundle)) $results = db_query("SELECT tid FROM {taxonomy_term_field_data} WHERE vid = :vid", array(':vid' => $bundle))
->fetchAllAssoc('tid'); ->fetchAllAssoc('tid');
$urls = array(); $urls = array();
foreach ($results as $id => $changed) { foreach ($results as $id => $changed) {
$urls[$id] = Url::fromRoute("entity.$entity_type.canonical", array('taxonomy_term' => $id), array( $urls[$id] = Url::fromRoute("entity.taxonomy_term.canonical", array('taxonomy_term' => $id), array(
'language' => $language, 'language' => $language,
'absolute' => TRUE 'absolute' => TRUE
))->toString(); ))->toString();
......
...@@ -30,19 +30,6 @@ class Simplesitemap { ...@@ -30,19 +30,6 @@ class Simplesitemap {
return FALSE; return FALSE;
} }
public static function get_entity_type_name($entity) {
if (method_exists($entity, 'getEntityType')) {
if (!$entity_type_name = $entity->getEntityType()->getBundleOf()) {
if (method_exists($entity, 'getEntityTypeId')) {
return $entity->getEntityTypeId();
}
}
return $entity->getEntityType()->getBundleOf();
}
return FALSE;
}
public static function get_plugin_path($entity_type_name) { public static function get_plugin_path($entity_type_name) {
$class_path = drupal_get_path('module', 'simplesitemap') . '/' . self::SITEMAP_PLUGIN_PATH . '/' . $entity_type_name . '.php'; $class_path = drupal_get_path('module', 'simplesitemap') . '/' . self::SITEMAP_PLUGIN_PATH . '/' . $entity_type_name . '.php';
if (file_exists($class_path)) { if (file_exists($class_path)) {
......
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