Commit 6d235a5d authored by gbyte.co's avatar gbyte.co

Clean up some

parent 169f2f9e
......@@ -11,7 +11,11 @@ use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\system\MenuInterface;
/**
* Implements hook_help.
*Implements hook_help.
*
* @param $route_name
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* @return \Drupal\Component\Render\MarkupInterface|null
*/
function simple_sitemap_help($route_name, RouteMatchInterface $route_match) {
return $route_name === 'help.page.simple_sitemap' ?
......@@ -22,14 +26,18 @@ function simple_sitemap_help($route_name, RouteMatchInterface $route_match) {
* Implements hook_form_alter.
*
* Adds sitemap settings to entity types that are supported via plugins.
*
* @param $form
* @param \Drupal\Core\Form\FormStateInterface $form_state
* @param $form_id
*/
function simple_sitemap_form_alter(&$form, FormStateInterface $form_state, $form_id) {
/**
* @var Drupal\simple_sitemap\Form\FormHelper $f
*/
$f = \Drupal::service('simple_sitemap.form_helper')->processForm($form_state);
if (!$f->alteringForm()) {
$f = \Drupal::service('simple_sitemap.form_helper');
if (!$f->processForm($form_state)) {
return;
}
......@@ -73,13 +81,19 @@ function simple_sitemap_form_alter(&$form, FormStateInterface $form_state, $form
/**
* Form submission handler called in hook_form_alter.
*
* @param $form
* @param \Drupal\Core\Form\FormStateInterface $form_state
*/
function simple_sitemap_entity_form_submit($form, FormStateInterface &$form_state) {
/**
* @var Drupal\simple_sitemap\Form\FormHelper $f
*/
$f = \Drupal::service('simple_sitemap.form_helper')->processForm($form_state);
$f = \Drupal::service('simple_sitemap.form_helper');
if (!$f->processForm($form_state)) {
return;
}
$values = $form_state->getValues();
......@@ -162,6 +176,9 @@ function simple_sitemap_entity_delete(EntityInterface $entity) {
* Implements hook_entity_bundle_delete().
*
* Removes settings of the removed bundle.
*
* @param string $entity_type_id
* @param string $bundle
*/
function simple_sitemap_entity_bundle_delete($entity_type_id, $bundle) {
simple_sitemap_delete_bundle_config($entity_type_id, $bundle);
......
......@@ -43,11 +43,15 @@ class CustomUrlGenerator extends UrlGeneratorBase implements UrlGeneratorInterfa
$path_data = [
'path' => $path,
'lastmod' => method_exists($entity, 'getChangedTime') ? date_iso8601($entity->getChangedTime()) : NULL,
'lastmod' => method_exists($entity, 'getChangedTime')
? date_iso8601($entity->getChangedTime()) : NULL,
'priority' => isset($custom_path['priority']) ? $custom_path['priority'] : NULL,
];
if (NULL !== $entity) {
$path_data['entity_info'] = ['entity_type' => $entity->getEntityTypeId(), 'id' => $entity->id()];
$path_data['entity_info'] = [
'entity_type' => $entity->getEntityTypeId(),
'id' => $entity->id()
];
}
$this->addUrlVariants($url_object, $path_data, $entity);
}
......
......@@ -55,8 +55,12 @@ class EntityUrlGenerator extends UrlGeneratorBase implements UrlGeneratorInterfa
$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,
'entity_info' => [
'entity_type' => $entity_info['entity_type_name'],
'id' => $entity_id
],
'lastmod' => method_exists($entity, 'getChangedTime')
? date_iso8601($entity->getChangedTime()) : NULL,
'priority' => $entity_settings['priority'],
];
$this->addUrlVariants($url_object, $path_data, $entity);
......
......@@ -55,7 +55,7 @@ class EntityHelper {
* @return array
* Objects of entity types that can be indexed by the sitemap.
*/
public function getSitemapEntityTypes() {
public function getSupportedEntityTypes() {
$entity_types = $this->entityTypeManager->getDefinitions();
foreach ($entity_types as $entity_type_id => $entity_type) {
......@@ -80,7 +80,7 @@ class EntityHelper {
return FALSE;
}
$sitemap_entity_types = $this->getSitemapEntityTypes();
$sitemap_entity_types = $this->getSupportedEntityTypes();
if (isset($sitemap_entity_types[$entity_type_id])) {
$entity_type = $sitemap_entity_types[$entity_type_id];
if (empty($entity_type->getBundleEntityType())) {
......
......@@ -2,11 +2,11 @@
namespace Drupal\simple_sitemap\Form;
use Drupal\Core\Form\FormStateInterface;
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
......@@ -39,15 +39,10 @@ class FormHelper {
*/
protected $formState;
/**
* @var bool
*/
protected $alteringForm = TRUE;
/**
* @var string|null
*/
protected $entityCategory = NULL;
protected $entityCategory;
/**
* @var string
......@@ -94,21 +89,13 @@ class FormHelper {
}
/**
* @param \Drupal\Core\Form\FormState $form_state
* @return $this
* @param \Drupal\Core\Form\FormStateInterface $form_state
* @return bool
*/
public function processForm(FormState $form_state) {
public function processForm(FormStateInterface $form_state) {
$this->formState = $form_state;
$this->getEntityDataFromFormEntity();
$this->assertAlteringForm();
return $this;
}
/**
* @return bool
*/
public function alteringForm() {
return $this->alteringForm;
return $this->supports();
}
/**
......@@ -176,31 +163,33 @@ class FormHelper {
}
/**
*
* @return bool
*/
protected function assertAlteringForm() {
protected function supports() {
// Do not alter the form if user lacks certain permissions.
if (!$this->currentUser->hasPermission('administer sitemap settings')) {
$this->alteringForm = FALSE;
return FALSE;
}
// Do not alter the form if it is irrelevant to sitemap generation.
elseif (empty($this->getEntityCategory())) {
$this->alteringForm = FALSE;
return FALSE;
}
// Do not alter the form if entity is not enabled in sitemap settings.
elseif (!$this->generator->entityTypeIsEnabled($this->getEntityTypeId())) {
$this->alteringForm = FALSE;
return FALSE;
}
// Do not alter the form, if sitemap is disabled for the entity type of this
// entity instance.
elseif ($this->getEntityCategory() == 'instance'
&& !$this->generator->bundleIsIndexed($this->getEntityTypeId(), $this->getBundleName())) {
$this->alteringForm = FALSE;
return FALSE;
}
return TRUE;
}
/**
......@@ -281,13 +270,16 @@ class FormHelper {
$form_entity = $this->getFormEntity();
if ($form_entity !== FALSE) {
$entity_type_id = $form_entity->getEntityTypeId();
$sitemap_entity_types = $this->entityHelper->getSitemapEntityTypes();
$sitemap_entity_types = $this->entityHelper->getSupportedEntityTypes();
if (isset($sitemap_entity_types[$entity_type_id])) {
$this->setEntityCategory('instance');
}
else {
foreach ($sitemap_entity_types as $sitemap_entity) {
if ($sitemap_entity->getBundleEntityType() == $entity_type_id) {
/**
* @var \Drupal\Core\Entity\EntityType $sitemap_entity_type
*/
foreach ($sitemap_entity_types as $sitemap_entity_type) {
if ($sitemap_entity_type->getBundleEntityType() == $entity_type_id) {
$this->setEntityCategory('bundle');
break;
}
......@@ -322,7 +314,7 @@ class FormHelper {
/**
* Gets the object entity of the form if available.
*
* @return object|false
* @return \Drupal\Core\Entity\Entity|false
* Entity or FALSE if non-existent or if form operation is
* 'delete'.
*/
......
......@@ -34,7 +34,7 @@ class SimplesitemapEntitiesForm extends SimplesitemapFormBase {
$form['#attached']['drupalSettings']['simple_sitemap'] = ['all_entities' => [], 'atomic_entities' => []];
$entity_type_labels = [];
foreach ($this->entityHelper->getSitemapEntityTypes() as $entity_type_id => $entity_type) {
foreach ($this->entityHelper->getSupportedEntityTypes() as $entity_type_id => $entity_type) {
$entity_type_labels[$entity_type_id] = $entity_type->getLabel() ? : $entity_type_id;
}
asort($entity_type_labels);
......
......@@ -313,7 +313,7 @@ class Simplesitemap {
//todo: Use addLinkSettings()?
// Delete entity overrides which are identical to new bundle setting.
$sitemap_entity_types = $this->entityHelper->getSitemapEntityTypes();
$sitemap_entity_types = $this->entityHelper->getSupportedEntityTypes();
if (isset($sitemap_entity_types[$entity_type_id])) {
$entity_type = $sitemap_entity_types[$entity_type_id];
$keys = $entity_type->getKeys();
......
......@@ -163,7 +163,7 @@ class SitemapGenerator {
*/
protected function getEntityTypeData() {
$data_sets = [];
$sitemap_entity_types = $this->entityHelper->getSitemapEntityTypes();
$sitemap_entity_types = $this->entityHelper->getSupportedEntityTypes();
$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