Commit 77f29dc2 authored by gbyte.co's avatar gbyte.co Committed by gbyte.co

Adding new plugin to support the user entity, giving plugins the possibility...

Adding new plugin to support the user entity, giving plugins the possibility to define lastmod parameter.
parent cf9bab90
...@@ -14,20 +14,41 @@ use Drupal\simplesitemap\SitemapGenerator; ...@@ -14,20 +14,41 @@ use Drupal\simplesitemap\SitemapGenerator;
*/ */
function simplesitemap_form_alter(&$form, $form_state, $form_id) { function simplesitemap_form_alter(&$form, $form_state, $form_id) {
$form_entity = Simplesitemap::get_form_entity($form_state); // Get all simplesitemap plugins.
if (!$form_entity)
return;
$entity_type_id = $form_entity->getEntityTypeId();
//todo: wrap into helper function
// Return if a sitemap plugin of this entity type does not exist.
$manager = \Drupal::service('plugin.manager.simplesitemap'); $manager = \Drupal::service('plugin.manager.simplesitemap');
$plugins = $manager->getDefinitions(); $plugins = $manager->getDefinitions();
if (!isset($plugins[$entity_type_id]))
return;
$bundle_name = $form_entity->Id(); // Go through simplesitemap plugins and check if one of them declares usage
// of this particular form. If that's the case, get 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) {
$entity_type_id = $plugin['id'];
$bundle_name = $entity_type_id;
}
}
// Else get entity type id and bundle name from the form if available and only
// if a simplesitemap plugin of the same entity type exists.
if (empty($entity_type_id)) {
$form_entity = Simplesitemap::get_form_entity($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_id = $form_entity_type_id;
$bundle_name = $form_entity->Id();
}
}
}
}
// If both methods of getting simplesitemap configuration for this form
// failed, return.
if (empty($entity_type_id))
return;
$sitemap = new Simplesitemap; $sitemap = new Simplesitemap;
...@@ -63,6 +84,9 @@ function simplesitemap_form_alter(&$form, $form_state, $form_id) { ...@@ -63,6 +84,9 @@ function simplesitemap_form_alter(&$form, $form_state, $form_id) {
'#default_value' => FALSE, '#default_value' => FALSE,
); );
$form['#simplesitemap']['entity_type_id'] = $entity_type_id;
$form['#simplesitemap']['bundle_name'] = $bundle_name;
// Add submission handler. // Add submission handler.
$form['actions']['submit']['#submit'][] = 'simplesitemap_entity_form_submit'; $form['actions']['submit']['#submit'][] = 'simplesitemap_entity_form_submit';
...@@ -74,16 +98,14 @@ function simplesitemap_form_alter(&$form, $form_state, $form_id) { ...@@ -74,16 +98,14 @@ function simplesitemap_form_alter(&$form, $form_state, $form_id) {
function simplesitemap_entity_form_submit($form, &$form_state) { function simplesitemap_entity_form_submit($form, &$form_state) {
$sitemap = new Simplesitemap; $sitemap = new Simplesitemap;
$values = $form_state->getValues(); $values = $form_state->getValues();
// Only make changes in DB if sitemap settings actually changed. // Only make changes in DB if sitemap settings actually changed.
if ($values['simplesitemap_index_content'] != $form['simplesitemap']['simplesitemap_index_content']['#default_value'] if ($values['simplesitemap_index_content'] != $form['simplesitemap']['simplesitemap_index_content']['#default_value']
|| $values['simplesitemap_priority'] != $form['simplesitemap']['simplesitemap_priority']['#default_value']) { || $values['simplesitemap_priority'] != $form['simplesitemap']['simplesitemap_priority']['#default_value']) {
$form_entity = Simplesitemap::get_form_entity($form_state); $entity_type_id = $form['#simplesitemap']['entity_type_id'];
$entity_type_id = $form_entity->getEntityTypeId(); $bundle_name = $form['#simplesitemap']['bundle_name'];
$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();
......
...@@ -24,8 +24,6 @@ class CustomLinkGenerator { ...@@ -24,8 +24,6 @@ class CustomLinkGenerator {
$paths = array(); $paths = array();
foreach($custom_paths as $i => $custom_path) { foreach($custom_paths as $i => $custom_path) {
if (!isset($custom_path['index']) || $custom_path['index']) { if (!isset($custom_path['index']) || $custom_path['index']) {
//Commented out as custom paths already come in the right format, just need to remove the prepending slash.
//$paths[$i]['path'] = Drupal\Core\Url::fromUserInput($custom_path['path'], array())->getInternalPath();
$paths[$i]['path'] = substr($custom_path['path'], 1); $paths[$i]['path'] = substr($custom_path['path'], 1);
$paths[$i]['priority'] = isset($custom_path['priority']) ? $custom_path['priority'] : NULL; $paths[$i]['priority'] = isset($custom_path['priority']) ? $custom_path['priority'] : NULL;
$paths[$i]['lastmod'] = NULL; //todo: implement $paths[$i]['lastmod'] = NULL; //todo: implement
......
...@@ -18,45 +18,18 @@ abstract class LinkGeneratorBase extends PluginBase implements LinkGeneratorInte ...@@ -18,45 +18,18 @@ abstract class LinkGeneratorBase extends PluginBase implements LinkGeneratorInte
public function get_entity_paths($entity_type, $bundles) { public function get_entity_paths($entity_type, $bundles) {
$i = 0; $i = 0;
foreach($bundles as $bundle => $bundle_settings) { foreach($bundles as $bundle => $bundle_settings) {
if (!$bundle_settings['index']) { if (!$bundle_settings['index'])
continue; continue;
} foreach ($this->get_entity_bundle_paths($bundle) as $id => $path) {
$paths = $this->get_entity_bundle_paths($bundle); $this->entity_paths[$i]['path'] = $path['path'];
foreach ($paths as $id => $link) { $this->entity_paths[$i]['priority'] = !empty($path['priority']) ? $path['priority'] : $bundle_settings['priority'];
$this->entity_paths[$i]['path'] = $link; $this->entity_paths[$i]['lastmod'] = !empty($path['lastmod']) ? date_iso8601($path['lastmod']) : NULL;
$this->entity_paths[$i]['priority'] = $bundle_settings['priority'];
$this->entity_paths[$i]['lastmod'] = $this->get_lastmod($entity_type, $id);
$i++; $i++;
} }
} }
return $this->entity_paths; return $this->entity_paths;
} }
/**
* Gets lastmod date for an entity.
*
* @param string $entity_type
* E.g. 'node_type', 'taxonomy_vocabulary'.
* @param int $id
* ID of the entity.
*
* @return string
* Lastmod date or NULL if none.
*/
private function get_lastmod($entity_type, $id) {
switch ($entity_type) {
case 'node_type':
$lastmod = db_query("SELECT changed FROM {node_field_data} WHERE nid = :nid LIMIT 1", array(':nid' => $id))->fetchCol();
break;
case 'taxonomy_vocabulary':
$lastmod = db_query("SELECT changed FROM {taxonomy_term_field_data} WHERE tid = :tid LIMIT 1", array(':tid' => $id))->fetchCol();
break;
case 'menu':
//todo: to be implemented
}
return isset($lastmod[0]) ? date_iso8601($lastmod[0]) : NULL;
}
/** /**
* Returns an array of all urls to this bundle. * Returns an array of all urls to this bundle.
* *
......
...@@ -28,7 +28,7 @@ class Menu extends LinkGeneratorBase { ...@@ -28,7 +28,7 @@ class Menu extends LinkGeneratorBase {
$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');
//todo: Url::getRouteParameters()? //todo: Use Url::getRouteParameters()?
$paths = array(); $paths = array();
foreach ($routes as $id => $entity) { foreach ($routes as $id => $entity) {
if (empty($entity->route_name)) if (empty($entity->route_name))
...@@ -37,8 +37,8 @@ class Menu extends LinkGeneratorBase { ...@@ -37,8 +37,8 @@ class Menu extends LinkGeneratorBase {
$options = !empty($route_parameters = unserialize($entity->route_parameters)) $options = !empty($route_parameters = unserialize($entity->route_parameters))
? array(key($route_parameters) => $route_parameters[key($route_parameters)]) : array(); ? array(key($route_parameters) => $route_parameters[key($route_parameters)]) : array();
$paths[$id] = Url::fromRoute($entity->route_name, $options, array( $paths[$id]['path'] = Url::fromRoute($entity->route_name, $options, array())->getInternalPath();
))->getInternalPath(); //todo: Implement lastmod for menu items.
} }
return $paths; return $paths;
} }
......
...@@ -25,12 +25,13 @@ class NodeType extends LinkGeneratorBase { ...@@ -25,12 +25,13 @@ class NodeType extends LinkGeneratorBase {
* {@inheritdoc} * {@inheritdoc}
*/ */
function get_entity_bundle_paths($bundle) { function get_entity_bundle_paths($bundle) {
$results = db_query("SELECT nid FROM {node_field_data} WHERE status = 1 AND type = :type", array(':type' => $bundle)) $results = db_query("SELECT nid, changed FROM {node_field_data} WHERE status = 1 AND type = :type", array(':type' => $bundle))
->fetchAllAssoc('nid'); ->fetchAllAssoc('nid');
$paths = array(); $paths = array();
foreach ($results as $id => $changed) { foreach ($results as $id => $data) {
$paths[$id] = Url::fromRoute("entity.node.canonical", array('node' => $id), array())->getInternalPath(); $paths[$id]['path'] = Url::fromRoute("entity.node.canonical", array('node' => $id), array())->getInternalPath();
$paths[$id]['lastmod'] = $data->changed;
} }
return $paths; return $paths;
} }
......
...@@ -25,12 +25,13 @@ class TaxonomyVocabulary extends LinkGeneratorBase { ...@@ -25,12 +25,13 @@ class TaxonomyVocabulary extends LinkGeneratorBase {
* {@inheritdoc} * {@inheritdoc}
*/ */
function get_entity_bundle_paths($bundle) { function get_entity_bundle_paths($bundle) {
$results = db_query("SELECT tid FROM {taxonomy_term_field_data} WHERE vid = :vid", array(':vid' => $bundle)) $results = db_query("SELECT tid, changed FROM {taxonomy_term_field_data} WHERE vid = :vid", array(':vid' => $bundle))
->fetchAllAssoc('tid'); ->fetchAllAssoc('tid');
$paths = array(); $paths = array();
foreach ($results as $id => $changed) { foreach ($results as $id => $data) {
$paths[$id] = Url::fromRoute("entity.taxonomy_term.canonical", array('taxonomy_term' => $id), array())->getInternalPath(); $paths[$id]['path'] = Url::fromRoute("entity.taxonomy_term.canonical", array('taxonomy_term' => $id), array())->getInternalPath();
$paths[$id]['lastmod'] = $data->changed;
} }
return $paths; return $paths;
} }
......
<?php
/**
* @file
* Contains \Drupal\simplesitemap\Plugin\LinkGenerator\User.
*
* Plugin for user link generation.
*/
namespace Drupal\simplesitemap\Plugin\LinkGenerator;
use Drupal\simplesitemap\Annotation\LinkGenerator;
use Drupal\simplesitemap\LinkGeneratorBase;
use Drupal\Core\Url;
/**
* User class.
*
* @LinkGenerator(
* id = "user",
* form_id = "user_admin_settings"
* )
*/
class User extends LinkGeneratorBase {
/**
* {@inheritdoc}
*/
function get_entity_bundle_paths($bundle) {
$results = db_query("SELECT uid, changed FROM {users_field_data} WHERE status = 1")
->fetchAllAssoc('uid');
$paths = array();
foreach ($results as $id => $data) {
$paths[$id]['path'] = Url::fromRoute("entity.user.canonical", array('user' => $id), array())->getInternalPath();
$paths[$id]['lastmod'] = $data->changed;
}
return $paths;
}
}
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