Commit cf9bab90 authored by gbyte.co's avatar gbyte.co Committed by gbyte.co

Issue #2657232 by jribeiro, gbyte.co: Duplicated links

parent cad9deed
......@@ -3,13 +3,11 @@
* @file
* Contains \Drupal\simplesitemap\LinkGenerators\CustomLinkGenerator.
*
* Generates custom sitemap links provided by the user.
* Generates custom sitemap paths provided by the user.
*/
namespace Drupal\simplesitemap;
use Drupal\Core\Url;
/**
* CustomLinkGenerator class.
*/
......@@ -19,24 +17,20 @@ class CustomLinkGenerator {
* Returns an array of all urls of the custom paths.
*
* @param array $custom_paths
* @param array $languages
* Array of Drupal language objects.
*
* @return array $urls
*/
public function get_custom_links($custom_paths, $languages) {
$links = array();
public function get_custom_paths($custom_paths) {
$paths = array();
foreach($custom_paths as $i => $custom_path) {
if (!isset($custom_path['index']) || $custom_path['index']) {
foreach($languages as $language) {
$links[$i]['url'][$language->getId()] = Url::fromUserInput($custom_path['path'], array(
'language' => $language,
'absolute' => TRUE
))->toString();
}
$links[$i]['priority'] = isset($custom_path['priority']) ? $custom_path['priority'] : NULL;
$links[$i]['lastmod'] = NULL; //todo: implement
//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]['priority'] = isset($custom_path['priority']) ? $custom_path['priority'] : NULL;
$paths[$i]['lastmod'] = NULL; //todo: implement
}
}
return $links;
return $paths;
}
}
......@@ -10,35 +10,38 @@ use Drupal\Component\Plugin\PluginBase;
abstract class LinkGeneratorBase extends PluginBase implements LinkGeneratorInterface {
private $entity_links = array();
private $entity_paths = array();
/**
* {@inheritdoc}
*/
public function get_entity_links($entity_type, $bundles, $languages) {
public function get_entity_paths($entity_type, $bundles) {
$i = 0;
foreach($bundles as $bundle => $bundle_settings) {
if (!$bundle_settings['index']) {
continue;
}
$links = $this->get_entity_bundle_links($bundle, $languages);
foreach ($links as $id => $link) {
$this->entity_links[$i]['url'] = $link;
$this->entity_links[$i]['priority'] = $bundle_settings['priority'];
$this->entity_links[$i]['lastmod'] = $this->get_lastmod($entity_type, $id);
$paths = $this->get_entity_bundle_paths($bundle);
foreach ($paths as $id => $link) {
$this->entity_paths[$i]['path'] = $link;
$this->entity_paths[$i]['priority'] = $bundle_settings['priority'];
$this->entity_paths[$i]['lastmod'] = $this->get_lastmod($entity_type, $id);
$i++;
}
}
return $this->entity_links;
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.
*
* @return string
* Lastmod date or NULL if none.
*/
private function get_lastmod($entity_type, $id) {
switch ($entity_type) {
......@@ -59,11 +62,11 @@ abstract class LinkGeneratorBase extends PluginBase implements LinkGeneratorInte
*
* @param string $bundle
* Machine name of the bundle, eg. 'page'.
* @param array $languages
* Array of Drupal language objects.
* @return array $urls
*
* @return array $paths
* A numeric array of Drupal internal paths like node/1/edit or user/1
*
* @abstract
*/
abstract function get_entity_bundle_links($bundle, $languages);
abstract function get_entity_bundle_paths($bundle);
}
<?php
/**
* @file
* Provides Drupal\simplesitemap\LinkGenerator.
* Contains Drupal\simplesitemap\LinkGeneratorInterface.
*/
namespace Drupal\simplesitemap;
......@@ -16,9 +15,12 @@ use Drupal\Component\Plugin\PluginInspectionInterface;
interface LinkGeneratorInterface extends PluginInspectionInterface {
/**
* @param string $entity_type
* E.g. 'node_type', 'taxonomy_vocabulary'.
* @param array $bundles
* E.g. 'page'.
*
*
* @return array
* @return array $paths
*/
public function get_entity_links($entity_type, $bundles, $languages);
public function get_entity_paths($entity_type, $bundles);
}
......@@ -24,22 +24,22 @@ class Menu extends LinkGeneratorBase {
/**
* {@inheritdoc}
*/
function get_entity_bundle_links($bundle, $languages) {
function get_entity_bundle_paths($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');
$urls = array();
//todo: Url::getRouteParameters()?
$paths = array();
foreach ($routes as $id => $entity) {
if (empty($entity->route_name))
continue;
$options = !empty($route_parameters = unserialize($entity->route_parameters)) ? array(key($route_parameters) => $route_parameters[key($route_parameters)]) : array();
foreach($languages as $language) {
$urls[$id][$language->getId()] = Url::fromRoute($entity->route_name, $options, array(
'language' => $language,
'absolute' => TRUE
))->toString();
}
$options = !empty($route_parameters = unserialize($entity->route_parameters))
? array(key($route_parameters) => $route_parameters[key($route_parameters)]) : array();
$paths[$id] = Url::fromRoute($entity->route_name, $options, array(
))->getInternalPath();
}
return $urls;
return $paths;
}
}
......@@ -24,19 +24,14 @@ class NodeType extends LinkGeneratorBase {
/**
* {@inheritdoc}
*/
function get_entity_bundle_links($bundle, $languages) {
function get_entity_bundle_paths($bundle) {
$results = db_query("SELECT nid FROM {node_field_data} WHERE status = 1 AND type = :type", array(':type' => $bundle))
->fetchAllAssoc('nid');
$urls = array();
$paths = array();
foreach ($results as $id => $changed) {
foreach($languages as $language) {
$urls[$id][$language->getId()] = Url::fromRoute("entity.node.canonical", array('node' => $id), array(
'language' => $language,
'absolute' => TRUE
))->toString();
}
$paths[$id] = Url::fromRoute("entity.node.canonical", array('node' => $id), array())->getInternalPath();
}
return $urls;
return $paths;
}
}
......@@ -24,19 +24,14 @@ class TaxonomyVocabulary extends LinkGeneratorBase {
/**
* {@inheritdoc}
*/
function get_entity_bundle_links($bundle, $languages) {
function get_entity_bundle_paths($bundle) {
$results = db_query("SELECT tid FROM {taxonomy_term_field_data} WHERE vid = :vid", array(':vid' => $bundle))
->fetchAllAssoc('tid');
$urls = array();
$paths = array();
foreach ($results as $id => $changed) {
foreach($languages as $language) {
$urls[$id][$language->getId()] = Url::fromRoute("entity.taxonomy_term.canonical", array('taxonomy_term' => $id), array(
'language' => $language,
'absolute' => TRUE
))->toString();
}
$paths[$id] = Url::fromRoute("entity.taxonomy_term.canonical", array('taxonomy_term' => $id), array())->getInternalPath();
}
return $urls;
return $paths;
}
}
<?php
/**
* @file
* Contains SimplesitemapManager.
* Contains \Drupal\simplesitemap\SimplesitemapManager.
*/
namespace Drupal\simplesitemap;
......
......@@ -9,6 +9,7 @@
namespace Drupal\simplesitemap;
use \XMLWriter;
use Drupal\Core\Url;
/**
* SitemapGenerator class.
......@@ -67,11 +68,14 @@ class SitemapGenerator {
*/
public function generate_sitemap($max_links = NULL) {
$this->generate_custom_links();
$this->generate_entity_links();
$this->generate_custom_paths();
$this->generate_entity_paths();
$this->generate_urls_from_paths();
$timestamp = time();
$sitemaps = array();
// Create sitemap chunks according to the max_links setting.
if (!empty($max_links) && count($this->links) > 0) {
foreach(array_chunk($this->links, $max_links) as $sitemap_id => $sitemap_links) {
$sitemaps[] = (object)[
......@@ -80,6 +84,7 @@ class SitemapGenerator {
];
}
}
// If max_link setting is not set, create just one sitemap.
else {
$sitemaps[] = (object)[
'sitemap_string' => $this->generate_sitemap_chunk($this->links),
......@@ -89,6 +94,21 @@ class SitemapGenerator {
return $sitemaps;
}
/**
* Generates multilingual urls for each path.
*/
private function generate_urls_from_paths() {
foreach($this->links as $i => $link) {
foreach($this->languages as $language) {
$this->links[$i]['url'][$language->getId()] = Url::fromUserInput('/' . $link['path'], array(
'language' => $language,
'absolute' => TRUE
))->toString();
}
}
}
/**
* Generates and returns the sitemap index.
*
......@@ -168,18 +188,18 @@ class SitemapGenerator {
}
/**
* Gets custom links.
* Generates custom internal paths.
*/
private function generate_custom_links() {
private function generate_custom_paths() {
$link_generator = new CustomLinkGenerator();
$links = $link_generator->get_custom_links($this->custom , $this->languages);
$this->links = array_merge($this->links, $links);
$links = $link_generator->get_custom_paths($this->custom);
$this->add_created_paths($links);
}
/**
* Gets entity type links.
* Makes all entity type link generating plugins add their paths.
*/
private function generate_entity_links() {
private function generate_entity_paths() {
$manager = \Drupal::service('plugin.manager.simplesitemap');
$plugins = $manager->getDefinitions();
......@@ -187,11 +207,27 @@ class SitemapGenerator {
foreach ($plugins as $link_generator_plugin) {
if (isset($this->entity_types[$link_generator_plugin['id']])) {
$instance = $manager->createInstance($link_generator_plugin['id']);
$links = $instance->get_entity_links($link_generator_plugin['id'],
$this->entity_types[$link_generator_plugin['id']],
$this->languages);
$this->links = array_merge($this->links, $links);
$links = $instance->get_entity_paths($link_generator_plugin['id'],
$this->entity_types[$link_generator_plugin['id']]);
$this->add_created_paths($links);
}
}
}
/**
* Adds Drupal internal paths generated by a plugin while removing duplicates.
*
* @param array $paths
* Drupal internal paths generated by a plugin.
*/
private function add_created_paths($paths) {
foreach($paths as $i => $path) {
foreach($this->links as $existing_path) {
if ($path['path'] == $existing_path['path']) {
unset($paths[$i]);
}
}
}
$this->links = array_merge($this->links, $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