Commit 8b8741c1 authored by gbyte.co's avatar gbyte.co

Simplify plugin structure and clean up variable names.

parent 2c8196e8
......@@ -79,7 +79,7 @@ class Batch {
foreach ($operations as $operation) {
$this->batch['operations'][] = array(
__CLASS__ . '::generateBundleUrls',
array($operation['query'], $operation['info'], $this->batchInfo)
array($operation['query'], $operation['entity_info'], $this->batchInfo)
);
};
break;
......@@ -115,52 +115,52 @@ class Batch {
* Batch callback function which generates urls to entity paths.
*
* @param object $query
* @param array $info
* @param array $entity_info
* @param array $batch_info
* @param array &$context
*
* @see https://api.drupal.org/api/drupal/core!includes!form.inc/group/batch/8
*/
public static function generateBundleUrls($query, $info, $batch_info, &$context) {
public static function generateBundleUrls($query, $entity_info, $batch_info, &$context) {
$languages = \Drupal::languageManager()->getLanguages();
$default_language_id = Simplesitemap::getDefaultLangId();
// Initializing batch.
if (empty($context['sandbox'])) {
self::InitializeBatch($query->countQuery()->execute()->fetchField(), $context);
self::InitializeBatch($query['query']->countQuery()->execute()->fetchField(), $context);
}
// Getting id field name from plugin info.
$fields = $query->getFields();
if (isset($info['field_info']['entity_id']) && isset($fields[$info['field_info']['entity_id']])) {
$id_field = $info['field_info']['entity_id'];
$fields = $query['query']->getFields();
if (isset($query['field_info']['entity_id']) && isset($fields[$query['field_info']['entity_id']])) {
$id_field = $query['field_info']['entity_id'];
}
else {
//todo: register error
}
// Getting the name of the route name field if any.
if (!empty($info['field_info']['route_name'])) {
$route_name_field = $info['field_info']['route_name'];
if (!empty($query['field_info']['route_name'])) {
$route_name_field = $query['field_info']['route_name'];
}
// Getting the name of the route parameter field if any.
if (!empty($info['field_info']['route_parameters'])) {
$route_params_field = $info['field_info']['route_parameters'];
if (!empty($query['field_info']['route_parameters'])) {
$route_params_field = $query['field_info']['route_parameters'];
}
// Creating a query limited to n=batch_process_limit entries.
$query->condition($id_field, $context['sandbox']['current_id'], '>')->orderBy($id_field);
$query['query']->condition($id_field, $context['sandbox']['current_id'], '>')->orderBy($id_field);
if (!empty($batch_info['batch_process_limit']))
$query->range(0, $batch_info['batch_process_limit']);
$result = $query->execute()->fetchAll();
$query['query']->range(0, $batch_info['batch_process_limit']);
$result = $query['query']->execute()->fetchAll();
foreach ($result as $row) {
self::SetCurrentId($row->$id_field, $context);
// Overriding entity settings if it has been overridden on entity edit page...
$bundle_name = !empty($info['bundle_settings']['bundle_name']) ? $info['bundle_settings']['bundle_name'] : NULL;
$bundle_entity_type = !empty($info['bundle_settings']['bundle_entity_type']) ? $info['bundle_settings']['bundle_entity_type'] : NULL;
$bundle_name = !empty($entity_info['bundle_name']) ? $entity_info['bundle_name'] : NULL;
$bundle_entity_type = !empty($entity_info['bundle_entity_type']) ? $entity_info['bundle_entity_type'] : NULL;
if (!empty($bundle_name) && !empty($bundle_entity_type)
&& isset($batch_info['entity_types'][$bundle_entity_type][$bundle_name]['entities'][$row->$id_field]['index'])) {
// Skipping entity if it has been excluded on entity edit page.
......@@ -175,16 +175,16 @@ class Batch {
if (isset($route_params_field) && !empty($route_parameters = unserialize($row->$route_params_field))) {
$route_parameters = array(key($route_parameters) => $route_parameters[key($route_parameters)]);
}
elseif (!empty($info['path_info']['entity_type'])) {
$route_parameters = array($info['path_info']['entity_type'] => $row->$id_field);
elseif (!empty($entity_info['entity_type_name'])) {
$route_parameters = array($entity_info['entity_type_name'] => $row->$id_field);
}
else {
$route_parameters = array();
}
// Getting the name of the options field if any.
if (!empty($info['field_info']['options'])) {
$options_field = $info['field_info']['options'];
if (!empty($query['field_info']['options'])) {
$options_field = $query['field_info']['options'];
}
// Setting options if they exist in the database (menu links)
......@@ -195,15 +195,16 @@ class Batch {
if (isset($route_name_field)) {
$route_name = $row->$route_name_field;
}
elseif (isset($info['path_info']['route_name'])) {
$route_name = $info['path_info']['route_name'];
elseif (!empty($entity_info['entity_type_name'])) {
$route_name = 'entity.' . $entity_info['entity_type_name'] . '.canonical';
}
else {
continue;
//todo: register error
}
$url_object = Url::fromRoute($route_name, $route_parameters, $options);
// Do not include path if anonymous users do not have access to it.
if (!$url_object->access($batch_info['anonymous_user_account']))
continue;
......@@ -227,8 +228,8 @@ class Batch {
'path' => $path,
'urls' => $urls,
'options' => $url_object->getOptions(),
'lastmod' => !empty($info['field_info']['lastmod']) ? date_iso8601($row->{$info['field_info']['lastmod']}) : NULL,
'priority' => isset($priority) ? $priority : (isset($info['bundle_settings']['priority']) ? $info['bundle_settings']['priority'] : NULL),
'lastmod' => !empty($query['field_info']['lastmod']) ? date_iso8601($row->{$query['field_info']['lastmod']}) : NULL,
'priority' => isset($priority) ? $priority : (isset($entity_info['bundle_settings']['priority']) ? $entity_info['bundle_settings']['priority'] : NULL),
);
$priority = NULL;
}
......
......@@ -17,9 +17,9 @@ interface LinkGeneratorInterface extends PluginInspectionInterface {
* Get metadata about the entities that the link generator is providing.
*
* @return array
* An array of information about the link generator.
* An array of information about fields provided by the query.
*/
public function getInfo();
public function getQueryInfo();
/**
* Get a non-executed query for entities of a specific bundle type.
......@@ -31,5 +31,4 @@ interface LinkGeneratorInterface extends PluginInspectionInterface {
* A query ready for execution.
*/
public function getQuery($bundle);
}
......@@ -8,14 +8,14 @@
namespace Drupal\simple_sitemap\Plugin\LinkGenerator;
use Drupal\simple_sitemap\Annotation\LinkGenerator;
use Drupal\simple_sitemap\LinkGeneratorBase;
/**
* CommerceProductType class.
*
* @LinkGenerator(
* id = "commerce_product_type"
* id = "commerce_product_type",
* entity_type_name = "commerce_product"
* )
*/
class CommerceProductType extends LinkGeneratorBase {
......@@ -23,16 +23,12 @@ class CommerceProductType extends LinkGeneratorBase {
/**
* {@inheritdoc}
*/
public function getInfo() {
public function getQueryInfo() {
return array(
'field_info' => array(
'entity_id' => 'product_id',
'lastmod' => 'changed',
),
'path_info' => array(
'route_name' => 'entity.commerce_product.canonical',
'entity_type' => 'commerce_product',
)
);
}
......
......@@ -8,7 +8,6 @@
namespace Drupal\simple_sitemap\Plugin\LinkGenerator;
use Drupal\simple_sitemap\Annotation\LinkGenerator;
use Drupal\simple_sitemap\LinkGeneratorBase;
/**
......@@ -23,7 +22,7 @@ class Menu extends LinkGeneratorBase {
/**
* {@inheritdoc}
*/
function getInfo() {
function getQueryInfo() {
return array(
'field_info' => array(
'entity_id' => 'mlid',
......@@ -31,7 +30,6 @@ class Menu extends LinkGeneratorBase {
'route_parameters' => 'route_parameters',
'options' => 'options',
),
'path_info' => array()
);
}
......
......@@ -8,14 +8,14 @@
namespace Drupal\simple_sitemap\Plugin\LinkGenerator;
use Drupal\simple_sitemap\Annotation\LinkGenerator;
use Drupal\simple_sitemap\LinkGeneratorBase;
/**
* NodeType class.
*
* @LinkGenerator(
* id = "node_type"
* id = "node_type",
* entity_type_name = "node"
* )
*/
class NodeType extends LinkGeneratorBase {
......@@ -23,16 +23,12 @@ class NodeType extends LinkGeneratorBase {
/**
* {@inheritdoc}
*/
public function getInfo() {
public function getQueryInfo() {
return array(
'field_info' => array(
'entity_id' => 'nid',
'lastmod' => 'changed',
),
'path_info' => array(
'route_name' => 'entity.node.canonical',
'entity_type' => 'node',
)
);
}
......
......@@ -8,14 +8,14 @@
namespace Drupal\simple_sitemap\Plugin\LinkGenerator;
use Drupal\simple_sitemap\Annotation\LinkGenerator;
use Drupal\simple_sitemap\LinkGeneratorBase;
/**
* TaxonomyVocabulary class.
*
* @LinkGenerator(
* id = "taxonomy_vocabulary"
* id = "taxonomy_vocabulary",
* entity_type_name = "taxonomy_term"
* )
*/
class TaxonomyVocabulary extends LinkGeneratorBase {
......@@ -23,16 +23,12 @@ class TaxonomyVocabulary extends LinkGeneratorBase {
/**
* {@inheritdoc}
*/
public function getInfo() {
public function getQueryInfo() {
return array(
'field_info' => array(
'entity_id' => 'tid',
'lastmod' => 'changed',
),
'path_info' => array(
'route_name' => 'entity.taxonomy_term.canonical',
'entity_type' => 'taxonomy_term',
)
);
}
......
......@@ -8,7 +8,6 @@
namespace Drupal\simple_sitemap\Plugin\LinkGenerator;
use Drupal\simple_sitemap\Annotation\LinkGenerator;
use Drupal\simple_sitemap\LinkGeneratorBase;
/**
......@@ -16,6 +15,7 @@ use Drupal\simple_sitemap\LinkGeneratorBase;
*
* @LinkGenerator(
* id = "user",
* entity_type_name = "user",
* form_id = "user_admin_settings"
* )
*/
......@@ -24,16 +24,12 @@ class User extends LinkGeneratorBase {
/**
* {@inheritdoc}
*/
public function getInfo() {
public function getQueryInfo() {
return array(
'field_info' => array(
'entity_id' => 'uid',
'lastmod' => 'changed',
),
'path_info' => array(
'route_name' => 'entity.user.canonical',
'entity_type' => 'user',
)
);
}
......
......@@ -76,11 +76,12 @@ class SitemapGenerator {
$instance = $manager->createInstance($link_generator_plugin['id']);
foreach($this->entityTypes[$link_generator_plugin['id']] as $bundle => $bundle_settings) {
if ($bundle_settings['index']) {
$operation['info'] = $instance->getInfo();
$operation['query'] = $instance->getQuery($bundle);
$operation['info']['bundle_settings'] = $bundle_settings;
$operation['info']['bundle_settings']['bundle_name'] = $bundle;
$operation['info']['bundle_settings']['bundle_entity_type'] = $link_generator_plugin['id'];
$operation['query']['query'] = $instance->getQuery($bundle);
$operation['query']['field_info'] = $instance->getQueryInfo()['field_info'];
$operation['entity_info']['bundle_settings'] = $bundle_settings;
$operation['entity_info']['bundle_name'] = $bundle;
$operation['entity_info']['bundle_entity_type'] = $link_generator_plugin['id'];
$operation['entity_info']['entity_type_name'] = !empty($link_generator_plugin['entity_type_name']) ? $link_generator_plugin['entity_type_name'] : '';
$operations[] = $operation;
}
}
......
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