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

Improve plugin interface

parent 35e4e2f3
......@@ -98,7 +98,7 @@ class Batch {
$operation[1][] = &$context;
call_user_func_array($operation[0], $operation[1]);
}
$this->finishGeneration(TRUE, $context['results'], []);
$this->finishGeneration(TRUE, !empty($context['results']) ? $context['results'] : [], []);
break;
}
}
......
......@@ -83,22 +83,18 @@ class ArbitraryUrlGenerator extends UrlGeneratorBase {
/**
* @return array
* @inheritdoc
*/
protected function getData() {
$arbitrary_links = [];
$this->moduleHandler->alter('simple_sitemap_arbitrary_links', $arbitrary_links);
return $arbitrary_links;
return array_values($arbitrary_links);
}
/**
* Batch function that adds arbitrary URLs to the sitemap.
* @inheritdoc
*/
public function generate() {
foreach ($this->getBatchIterationElements(array_values($this->getData())) as $i => $path_data) {
$this->setCurrentId($i);
$this->addUrl($path_data);
}
$this->processSegment();
protected function getPathData($path_data) {
return $path_data;
}
}
......@@ -95,54 +95,58 @@ class CustomUrlGenerator extends UrlGeneratorBase {
}
/**
* @return array
* @inheritdoc
*/
protected function getData() {
return $this->generator->getCustomLinks();
$this->includeImages = $this->generator->getSetting('custom_links_include_images', FALSE);
return array_values($this->generator->getCustomLinks());
}
/**
* Batch function which generates urls to custom paths.
* @inheritdoc
*/
public function generate() {
$this->includeImages = $this->generator->getSetting('custom_links_include_images', FALSE);
foreach ($this->getBatchIterationElements($this->getData()) as $i => $custom_path) {
$this->setCurrentId($i);
protected function getPathData($data) {
// todo: Change to different function, as this also checks if current user has access. The user however varies depending if process was started from the web interface or via cron/drush. Use getUrlIfValidWithoutAccessCheck()?
if (!$this->pathValidator->isValid($custom_path['path'])) {
// if (!(bool) $this->pathValidator->getUrlIfValidWithoutAccessCheck($custom_path['path'])) {
if (!$this->pathValidator->isValid($data['path'])) {
// if (!(bool) $this->pathValidator->getUrlIfValidWithoutAccessCheck($data['path'])) {
$this->logger->m(self::PATH_DOES_NOT_EXIST_OR_NO_ACCESS_MESSAGE,
['@path' => $custom_path['path'], '@custom_paths_url' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap/custom'])
['@path' => $data['path'], '@custom_paths_url' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap/custom'])
->display('warning', 'administer sitemap settings')
->log('warning');
continue;
return FALSE;
}
$url_object = Url::fromUserInput($custom_path['path'], ['absolute' => TRUE]);
$path = $url_object->getInternalPath();
if ($this->batchSettings['remove_duplicates'] && $this->pathProcessed($path)) {
continue;
if ($this->batchSettings['remove_duplicates'] && $this->pathProcessed($data['path'])) {
return FALSE;
}
$url_object = Url::fromUserInput($data['path'], ['absolute' => TRUE]);
$entity = $this->entityHelper->getEntityFromUrlObject($url_object);
$path_data = [
'path' => $path,
'url' => $url_object,
'lastmod' => method_exists($entity, 'getChangedTime')
? date_iso8601($entity->getChangedTime()) : NULL,
'priority' => isset($custom_path['priority']) ? $custom_path['priority'] : NULL,
'changefreq' => !empty($custom_path['changefreq']) ? $custom_path['changefreq'] : NULL,
'priority' => isset($data['priority']) ? $data['priority'] : NULL,
'changefreq' => !empty($data['changefreq']) ? $data['changefreq'] : NULL,
'images' => $this->includeImages && method_exists($entity, 'getEntityTypeId')
? $this->getImages($entity->getEntityTypeId(), $entity->id())
: []
: [],
'meta' => [
'path' => $data['path'],
]
];
$this->addUrl($path_data, $url_object);
}
$this->processSegment();
// Additional info useful in hooks.
if (NULL !== $entity) {
$path_data['meta']['entity_info'] = [
'entity_type' => $entity->getEntityTypeId(),
'id' => $entity->id(),
];
}
return $path_data;
}
}
......@@ -13,7 +13,7 @@ namespace Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator;
class EntityUrlGenerator extends UrlGeneratorBase {
/**
* @return array
* @inheritdoc
*/
protected function getData() {
$data_sets = [];
......@@ -41,7 +41,7 @@ class EntityUrlGenerator extends UrlGeneratorBase {
}
/**
* Batch callback function which generates urls to entity paths.
* @inheritdoc
*/
public function generate() {
......@@ -49,65 +49,81 @@ class EntityUrlGenerator extends UrlGeneratorBase {
foreach ($this->getBatchIterationElements($entity_info) as $entity_id => $entity) {
$this->setCurrentId($entity_id);
$entity_settings = $this->generator->getEntityInstanceSettings($entity_info['entity_type_name'], $entity_id);
if (empty($entity_settings['index'])) {
$path_data = $this->getPathData($entity);
if (!$path_data) {
continue;
}
$this->addUrl($path_data);
}
$this->processSegment();
}
}
switch ($entity_info['entity_type_name']) {
// Loading url object for menu links.
case 'menu_link_content':
if (!$entity->isEnabled()) {
continue 2;
}
$url_object = $entity->getUrlObject();
break;
// Loading url object for other entities.
default:
$url_object = $entity->toUrl();
}
/**
* @inheritdoc
*/
protected function getPathData($entity) {
// Do not include external paths.
if (!$url_object->isRouted()) {
continue;
}
$entity_id = $entity->id();
$entity_type_name = $entity->getEntityTypeId();
$path = $url_object->getInternalPath();
$entity_settings = $this->generator->getEntityInstanceSettings($entity_type_name, $entity_id);
// Do not include paths that have been already indexed.
if ($this->batchSettings['remove_duplicates'] && $this->pathProcessed($path)) {
continue;
if (empty($entity_settings['index'])) {
return FALSE;
}
switch ($entity_type_name) {
// Loading url object for menu links.
case 'menu_link_content':
if (!$entity->isEnabled()) {
return FALSE;
}
$url_object = $entity->getUrlObject();
break;
$url_object->setOption('absolute', TRUE);
$path_data = [
'path' => $path,
'entity_info' => [
'entity_type' => $entity_info['entity_type_name'],
'id' => $entity_id
],
'lastmod' => method_exists($entity, 'getChangedTime')
? date_iso8601($entity->getChangedTime()) : NULL,
'priority' => isset($entity_settings['priority']) ? $entity_settings['priority'] : NULL,
'changefreq' => !empty($entity_settings['changefreq']) ? $entity_settings['changefreq'] : NULL,
'images' => !empty($entity_settings['include_images'])
? $this->getImages($entity_info['entity_type_name'], $entity_id)
: []
];
$this->addUrl($path_data, $url_object);
}
$this->processSegment();
// Loading url object for other entities.
default:
$url_object = $entity->toUrl();
}
// Do not include external paths.
if (!$url_object->isRouted()) {
return FALSE;
}
$path = $url_object->getInternalPath();
// Do not include paths that have been already indexed.
if ($this->batchSettings['remove_duplicates'] && $this->pathProcessed($path)) {
return FALSE;
}
$url_object->setOption('absolute', TRUE);
return [
'url' => $url_object,
'lastmod' => method_exists($entity, 'getChangedTime')
? date_iso8601($entity->getChangedTime()) : NULL,
'priority' => isset($entity_settings['priority']) ? $entity_settings['priority'] : NULL,
'changefreq' => !empty($entity_settings['changefreq']) ? $entity_settings['changefreq'] : NULL,
'images' => !empty($entity_settings['include_images'])
? $this->getImages($entity_type_name, $entity_id)
: [],
// Additional info useful in hooks.
'meta' => [
'path' => $path,
'entity_info' => [
'entity_type' => $entity_type_name,
'id' => $entity_id,
],
]
];
}
/**
* @param array $entity_info
* @return \Drupal\Core\Entity\EntityInterface[]
* @inheritdoc
*/
protected function getBatchIterationElements(array $entity_info) {
$query = $this->entityTypeManager->getStorage($entity_info['entity_type_name'])->getQuery();
......
......@@ -179,10 +179,11 @@ abstract class UrlGeneratorBase extends PluginBase implements PluginInspectionIn
/**
* @param array $path_data
* @param \Drupal\Core\Url|NULL $url_object
*/
protected function addUrl(array $path_data, Url $url_object = NULL) {
if ($url_object !== NULL) {
protected function addUrl(array $path_data) {
if ($path_data['url'] instanceof Url) {
$url_object = $path_data['url'];
unset($path_data['url']);
$this->addUrlVariants($path_data, $url_object);
}
else {
......@@ -337,6 +338,32 @@ abstract class UrlGeneratorBase extends PluginBase implements PluginInspectionIn
: $elements;
}
/**
* @return mixed
*/
abstract protected function getData();
/**
* @param $path_data
* @return array
*/
abstract protected function getPathData($path_data);
/**
* Called by batch.
*/
public function generate() {
foreach ($this->getBatchIterationElements($this->getData()) as $id => $data) {
$this->setCurrentId($id);
$path_data = $this->getPathData($data);
if (!$path_data) {
continue;
}
$this->addUrl($path_data);
}
$this->processSegment();
}
/**
* @param $entity_type_name
* @param $entity_id
......
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