Commit 21a9b8fe authored by gbyte.co's avatar gbyte.co

Slim down Simplesitemap service by adding EntityHelper service

parent 1728ef6b
......@@ -55,7 +55,9 @@ function simple_sitemap_form_alter(&$form, FormStateInterface $form_state, $form
// Add submission handler.
if (isset($form['actions']['submit']['#submit'])) {
foreach (array_keys($form['actions']) as $action) {
if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
if ($action != 'preview'
&& isset($form['actions'][$action]['#type'])
&& $form['actions'][$action]['#type'] === 'submit') {
$form['actions'][$action]['#submit'][] = 'simple_sitemap_entity_form_submit';
}
}
......
......@@ -4,6 +4,7 @@ services:
public: true
arguments:
- '@simple_sitemap.sitemap_generator'
- '@simple_sitemap.entity_helper'
- '@config.factory'
- '@database'
- '@entity.query'
......@@ -16,15 +17,23 @@ services:
public: false
arguments:
- '@simple_sitemap.batch'
- '@simple_sitemap.entity_helper'
- '@database'
- '@module_handler'
- '@language_manager'
simple_sitemap.entity_helper:
class: Drupal\simple_sitemap\EntityHelper
public: true
arguments:
- '@entity_type.manager'
simple_sitemap.form_helper:
class: Drupal\simple_sitemap\Form\FormHelper
public: true
arguments:
- '@simple_sitemap.generator'
- '@simple_sitemap.entity_helper'
- '@current_user'
simple_sitemap.batch:
......
<?php
namespace Drupal\simple_sitemap;
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Entity;
/**
* Class EntityHelper
* @package Drupal\simple_sitemap
*/
class EntityHelper {
/**
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
private $entityTypeManager;
/**
* EntityHelper constructor.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
*/
public function __construct(EntityTypeManagerInterface $entityTypeManager) {
$this->entityTypeManager = $entityTypeManager;
}
/**
* Gets an entity's bundle name.
*
* @param \Drupal\Core\Entity\Entity $entity
* @return string
*/
public function getEntityInstanceBundleName(Entity $entity) {
return $entity->getEntityTypeId() == 'menu_link_content'
// Menu fix.
? $entity->getMenuName() : $entity->bundle();
}
/**
* Gets the entity type id for a bundle.
*
* @param \Drupal\Core\Entity\Entity $entity
* @return null|string
*/
public function getBundleEntityTypeId(Entity $entity) {
return $entity->getEntityTypeId() == 'menu'
// Menu fix.
? 'menu_link_content' : $entity->getEntityType()->getBundleOf();
}
/**
* Returns objects of entity types that can be indexed.
*
* @return array
* Objects of entity types that can be indexed by the sitemap.
*/
public function getSitemapEntityTypes() {
$entity_types = $this->entityTypeManager->getDefinitions();
foreach ($entity_types as $entity_type_id => $entity_type) {
if (!$entity_type instanceof ContentEntityTypeInterface
|| !method_exists($entity_type, 'getBundleEntityType')
|| !$entity_type->hasLinkTemplate('canonical')) {
unset($entity_types[$entity_type_id]);
}
}
return $entity_types;
}
/**
* Checks whether an entity type does not provide bundles.
*
* @param string $entity_type_id
* @return bool
*/
public function entityTypeIsAtomic($entity_type_id) {
// Menu fix.
if ($entity_type_id == 'menu_link_content') {
return FALSE;
}
$sitemap_entity_types = $this->getSitemapEntityTypes();
if (isset($sitemap_entity_types[$entity_type_id])) {
$entity_type = $sitemap_entity_types[$entity_type_id];
if (empty($entity_type->getBundleEntityType())) {
return TRUE;
}
}
// todo: throw exception.
return FALSE;
}
}
......@@ -3,8 +3,10 @@
namespace Drupal\simple_sitemap\Form;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\simple_sitemap\EntityHelper;
use Drupal\simple_sitemap\Simplesitemap;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Form\FormState;
/**
* Class FormHelper.
......@@ -19,6 +21,7 @@ class FormHelper {
const PRIORITY_DIVIDER = 10;
private $generator;
private $entityHelper;
private $currentUser;
private $formState;
......@@ -42,16 +45,18 @@ class FormHelper {
];
/**
* Form constructor.
*
* @param $generator
* @param $current_user
* FormHelper constructor.
* @param \Drupal\simple_sitemap\Simplesitemap $generator
* @param \Drupal\simple_sitemap\EntityHelper $entityHelper
* @param \Drupal\Core\Session\AccountProxyInterface $current_user
*/
public function __construct(
Simplesitemap $generator,
EntityHelper $entityHelper,
AccountProxyInterface $current_user
) {
$this->generator = $generator;
$this->entityHelper = $entityHelper;
$this->currentUser = $current_user;
}
......@@ -59,7 +64,7 @@ class FormHelper {
* @param $form_state
* @return $this
*/
public function processForm($form_state) {
public function processForm(FormState $form_state) {
$this->formState = $form_state;
if (!is_null($this->formState)) {
$this->getEntityDataFromFormEntity();
......@@ -148,11 +153,11 @@ class FormHelper {
}
/**
* @param $form_fragment
* @param array $form_fragment
* @param bool $multiple
* @return $this
*/
public function displayEntitySettings(&$form_fragment, $multiple = FALSE) {
public function displayEntitySettings(array &$form_fragment, $multiple = FALSE) {
$prefix = $multiple ? $this->entityTypeId . '_' : '';
if ($this->entityCategory == 'instance') {
......@@ -210,7 +215,7 @@ class FormHelper {
$form_entity = $this->getFormEntity();
if ($form_entity !== FALSE) {
$entity_type_id = $form_entity->getEntityTypeId();
$sitemap_entity_types = $this->generator->getSitemapEntityTypes();
$sitemap_entity_types = $this->entityHelper->getSitemapEntityTypes();
if (isset($sitemap_entity_types[$entity_type_id])) {
$this->entityCategory = 'instance';
}
......@@ -228,14 +233,14 @@ class FormHelper {
switch ($this->entityCategory) {
case 'bundle':
$this->entityTypeId = $this->generator->getBundleEntityTypeId($form_entity);
$this->entityTypeId = $this->entityHelper->getBundleEntityTypeId($form_entity);
$this->bundleName = $form_entity->id();
$this->instanceId = NULL;
break;
case 'instance':
$this->entityTypeId = $entity_type_id;
$this->bundleName = $this->generator->getEntityInstanceBundleName($form_entity);
$this->bundleName = $this->entityHelper->getEntityInstanceBundleName($form_entity);
// New menu link's id is '' instead of NULL, hence checking for empty.
$this->instanceId = !empty($form_entity->id()) ? $form_entity->id() : NULL;
break;
......@@ -281,12 +286,12 @@ class FormHelper {
* To be used in an entity form submit.
*
* @param $form
* @param $values
* @param array $values
*
* @return bool
* TRUE if simple_sitemap form values have been altered by the user.
*/
public function valuesChanged($form, $values) {
public function valuesChanged($form, array $values) {
foreach (self::$valuesToCheck as $field_name) {
if (isset($values[$field_name]) && $values[$field_name] != $form['simple_sitemap'][$field_name]['#default_value']) {
return TRUE;
......@@ -310,7 +315,7 @@ class FormHelper {
}
/**
* @param $priority
* @param string $priority
* @return string
*/
public function formatPriority($priority) {
......@@ -318,7 +323,7 @@ class FormHelper {
}
/**
* @param $priority
* @param string|int $priority
* @return bool
*/
public static function isValidPriority($priority) {
......
......@@ -35,7 +35,7 @@ class SimplesitemapEntitiesForm extends SimplesitemapFormBase {
$form['#attached']['drupalSettings']['simple_sitemap'] = ['all_entities' => [], 'atomic_entities' => []];
$entity_type_labels = [];
foreach ($this->generator->getSitemapEntityTypes() as $entity_type_id => $entity_type) {
foreach ($this->entityHelper->getSitemapEntityTypes() as $entity_type_id => $entity_type) {
$entity_type_labels[$entity_type_id] = $entity_type->getLabel() ? : $entity_type_id;
}
asort($entity_type_labels);
......@@ -68,7 +68,7 @@ class SimplesitemapEntitiesForm extends SimplesitemapFormBase {
$form['#attached']['drupalSettings']['simple_sitemap']['all_entities'][] = $css_entity_type_id;
if ($this->generator->entityTypeIsAtomic($entity_type_id)) {
if ($this->entityHelper->entityTypeIsAtomic($entity_type_id)) {
$form['simple_sitemap_entities']['entities'][$entity_type_id][$entity_type_id . '_enabled']['#description'] = $this->t('Sitemap settings for this entity type can be set below and overridden on its entity pages.');
$this->formHelper->setEntityCategory('bundle')
->setEntityTypeId($entity_type_id)
......@@ -93,7 +93,7 @@ class SimplesitemapEntitiesForm extends SimplesitemapFormBase {
$entity_type_id = substr($field_name, 0, -8);
if ($value) {
$this->generator->enableEntityType($entity_type_id);
if ($this->generator->entityTypeIsAtomic($entity_type_id)) {
if ($this->entityHelper->entityTypeIsAtomic($entity_type_id)) {
$this->generator->setBundleSettings($entity_type_id, $entity_type_id, [
'index' => TRUE,
'priority' => $values[$entity_type_id . '_simple_sitemap_priority'],
......
......@@ -2,6 +2,7 @@
namespace Drupal\simple_sitemap\Form;
use Drupal\simple_sitemap\EntityHelper;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\simple_sitemap\Simplesitemap;
......@@ -16,22 +17,25 @@ abstract class SimplesitemapFormBase extends ConfigFormBase {
protected $generator;
protected $formHelper;
protected $entityHelper;
protected $pathValidator;
/**
* SimplesitemapFormBase constructor.
*
* @param \Drupal\simple_sitemap\Simplesitemap $generator
* @param \Drupal\simple_sitemap\Form\FormHelper $form_helper
* @param \Drupal\simple_sitemap\EntityHelper $entity_helper
* @param \Drupal\Core\Path\PathValidator $path_validator
*/
public function __construct(
Simplesitemap $generator,
FormHelper $form_helper,
EntityHelper $entity_helper,
PathValidator $path_validator
) {
$this->generator = $generator;
$this->formHelper = $form_helper;
$this->entityHelper = $entity_helper;
$this->pathValidator = $path_validator;
}
......@@ -42,6 +46,7 @@ abstract class SimplesitemapFormBase extends ConfigFormBase {
return new static(
$container->get('simple_sitemap.generator'),
$container->get('simple_sitemap.form_helper'),
$container->get('simple_sitemap.entity_helper'),
$container->get('path.validator')
);
}
......
......@@ -2,7 +2,6 @@
namespace Drupal\simple_sitemap;
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\simple_sitemap\Form\FormHelper;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeManagerInterface;
......@@ -19,6 +18,7 @@ use Drupal\Core\Datetime\DateFormatter;
class Simplesitemap {
private $sitemapGenerator;
private $entityHelper;
private $configFactory;
private $db;
private $entityQuery;
......@@ -32,6 +32,7 @@ class Simplesitemap {
/**
* Simplesitemap constructor.
* @param \Drupal\simple_sitemap\SitemapGenerator $sitemapGenerator
* @param \Drupal\simple_sitemap\EntityHelper $entityHelper
* @param \Drupal\Core\Config\ConfigFactory $configFactory
* @param \Drupal\Core\Database\Connection $database
* @param \Drupal\Core\Entity\Query\QueryFactory $entityQuery
......@@ -41,6 +42,7 @@ class Simplesitemap {
*/
public function __construct(
SitemapGenerator $sitemapGenerator,
EntityHelper $entityHelper,
ConfigFactory $configFactory,
Connection $database,
QueryFactory $entityQuery,
......@@ -49,6 +51,7 @@ class Simplesitemap {
DateFormatter $dateFormatter
) {
$this->sitemapGenerator = $sitemapGenerator;
$this->entityHelper = $entityHelper;
$this->configFactory = $configFactory;
$this->db = $database;
$this->entityQuery = $entityQuery;
......@@ -102,7 +105,7 @@ class Simplesitemap {
// Updating settings.
$enabled_entity_types = $this->getSetting('enabled_entity_types');
if (($key = array_search($entity_type_id, $enabled_entity_types)) !== FALSE) {
if (FALSE !== ($key = array_search($entity_type_id, $enabled_entity_types))) {
unset ($enabled_entity_types[$key]);
$this->saveSetting('enabled_entity_types', array_values($enabled_entity_types));
}
......@@ -148,14 +151,14 @@ class Simplesitemap {
//todo: Use addLinkSettings()?
// Delete entity overrides which are identical to new bundle setting.
$sitemap_entity_types = $this->getSitemapEntityTypes();
$sitemap_entity_types = $this->entityHelper->getSitemapEntityTypes();
if (isset($sitemap_entity_types[$entity_type_id])) {
$entity_type = $sitemap_entity_types[$entity_type_id];
$keys = $entity_type->getKeys();
$keys['bundle'] = $entity_type_id == 'menu_link_content' ? 'menu_name' : $keys['bundle'];
$query = $this->entityQuery->get($entity_type_id);
if (!$this->entityTypeIsAtomic($entity_type_id)) {
if (!$this->entityHelper->entityTypeIsAtomic($entity_type_id)) {
$query->condition($keys['bundle'], $bundle_name);
}
$entity_ids = $query->execute();
......@@ -169,10 +172,8 @@ class Simplesitemap {
if (!empty($entity_ids)) {
$query->condition('o.entity_id', $entity_ids, 'IN');
}
$results = $query->execute()
->fetchAll();
foreach($results as $result) {
foreach($query->execute()->fetchAll() as $result) {
$delete = TRUE;
$instance_settings = unserialize($result->inclusion_settings);
foreach ($instance_settings as $setting_key => $instance_setting) {
......@@ -239,7 +240,7 @@ class Simplesitemap {
public function setEntityInstanceSettings($entity_type_id, $id, $settings) {
$entity = $this->entityTypeManager->getStorage($entity_type_id)->load($id);
$bundle_name = $this->getEntityInstanceBundleName($entity);
$bundle_name = $this->entityHelper->getEntityInstanceBundleName($entity);
$bundle_settings = $this->configFactory
->get("simple_sitemap.bundle_settings.$entity_type_id.$bundle_name")
->get();
......@@ -280,12 +281,12 @@ class Simplesitemap {
/**
* Gets sitemap settings for an entity instance which overrides the sitemap
* settings of its bundle.
* settings of its bundle, or bundle settings, if they are not overridden.
*
* @param string $entity_type_id
* @param int $id
*
* @return array
* @return array|false
*/
public function getEntityInstanceSettings($entity_type_id, $id) {
$results = $this->db->select('simple_sitemap_entity_overrides', 'o')
......@@ -299,9 +300,12 @@ class Simplesitemap {
return unserialize($results);
}
else {
$entity = $this->entityTypeManager->getStorage($entity_type_id)->load($id);
$bundle_name = $this->getEntityInstanceBundleName($entity);
return $this->getBundleSettings($entity_type_id, $bundle_name);
$entity = $this->entityTypeManager->getStorage($entity_type_id)
->load($id);
return $this->getBundleSettings(
$entity_type_id,
$this->entityHelper->getEntityInstanceBundleName($entity)
);
}
}
......@@ -464,30 +468,6 @@ class Simplesitemap {
return $this;
}
/**
* Gets an entity's bundle name.
*
* @param string $entity
* @return string
*/
public function getEntityInstanceBundleName($entity) {
return $entity->getEntityTypeId() == 'menu_link_content'
// Menu fix.
? $entity->getMenuName() : $entity->bundle();
}
/**
* Gets the entity type id for a bundle.
*
* @param string $entity
* @return string
*/
public function getBundleEntityTypeId($entity) {
return $entity->getEntityTypeId() == 'menu'
// Menu fix.
? 'menu_link_content' : $entity->getEntityType()->getBundleOf();
}
/**
* Returns the whole sitemap, a requested sitemap chunk,
* or the sitemap index file.
......@@ -601,47 +581,4 @@ class Simplesitemap {
}
return FALSE;
}
/**
* Returns objects of entity types that can be indexed.
*
* @return array
* Objects of entity types that can be indexed by the sitemap.
*/
public function getSitemapEntityTypes() {
$entity_types = $this->entityTypeManager->getDefinitions();
foreach ($entity_types as $entity_type_id => $entity_type) {
if (!$entity_type instanceof ContentEntityTypeInterface
|| !method_exists($entity_type, 'getBundleEntityType')
|| !$entity_type->hasLinkTemplate('canonical')) {
unset($entity_types[$entity_type_id]);
}
}
return $entity_types;
}
/**
* Checks whether an entity type does not provide bundles.
*
* @param string $entity_type_id
* @return bool
*/
public function entityTypeIsAtomic($entity_type_id) {
// Menu fix.
if ($entity_type_id == 'menu_link_content') {
return FALSE;
}
$sitemap_entity_types = $this->getSitemapEntityTypes();
if (isset($sitemap_entity_types[$entity_type_id])) {
$entity_type = $sitemap_entity_types[$entity_type_id];
if (empty($entity_type->getBundleEntityType())) {
return TRUE;
}
}
// todo: throw exception.
return FALSE;
}
}
......@@ -22,6 +22,7 @@ class SitemapGenerator {
const GENERATED_BY = 'Generated by the Simple XML sitemap Drupal module: https://drupal.org/project/simple_sitemap.';
private $batch;
private $entityHelper;
private $db;
private $moduleHandler;
private $defaultLanguageId;
......@@ -32,17 +33,20 @@ class SitemapGenerator {
/**
* SitemapGenerator constructor.
* @param \Drupal\simple_sitemap\Batch\Batch $batch
* @param \Drupal\simple_sitemap\EntityHelper $entityHelper
* @param \Drupal\Core\Database\Connection $database
* @param \Drupal\Core\Extension\ModuleHandler $module_handler
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
*/
public function __construct(
Batch $batch,
EntityHelper $entityHelper,
Connection $database,
ModuleHandler $module_handler,
LanguageManagerInterface $language_manager
) {
$this->batch = $batch;
$this->entityHelper = $entityHelper;
$this->db = $database;
$this->moduleHandler = $module_handler;
$this->defaultLanguageId = $language_manager->getDefaultLanguage()->getId();
......@@ -116,7 +120,7 @@ class SitemapGenerator {
*/
private function getEntityTypeData() {
$data_sets = [];
$sitemap_entity_types = $this->generator->getSitemapEntityTypes();
$sitemap_entity_types = $this->entityHelper->getSitemapEntityTypes();
$entity_types = $this->generator->getBundleSettings();
foreach ($entity_types as $entity_type_name => $bundles) {
if (isset($sitemap_entity_types[$entity_type_name])) {
......
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