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

Clean up some more

parent f4d4706a
......@@ -21,26 +21,24 @@ function simple_sitemap_help($route_name, \Drupal\Core\Routing\RouteMatchInterfa
*/
function simple_sitemap_form_alter(&$form, $form_state, $form_id) {
$f = new Form($form_state);
if ($f->alteringForm) {
$form['simple_sitemap'] = [
'#type' => 'details',
'#group' => isset($form['additional_settings']) ? 'additional_settings' : 'advanced',
'#title' => t('Simple XML sitemap'),
'#description' => $f->entityCategory == 'instance' ? t('Settings for this specific entity can be overridden here.') : '',
];
// Attach some js magic to forms.
if ($f->entityTypeId != 'comment' || $f->entityCategory != 'instance') // todo: JS not working on comment entity form, hence disabling.
$form['#attached']['library'][] = 'simple_sitemap/form';
// Only attach fieldset summary js to 'additional settings' vertical tabs.
if (isset($form['additional_settings'])) {
$form['#attached']['library'][] = 'simple_sitemap/fieldsetSummaries';
}
$f->displayEntitySitemapSettings($form['simple_sitemap']);
$f->displaySitemapRegenerationSetting($form['simple_sitemap']);
}
else {
if (!$f->alteringForm)
return;
$form['simple_sitemap'] = [
'#type' => 'details',
'#group' => isset($form['additional_settings']) ? 'additional_settings' : 'advanced',
'#title' => t('Simple XML sitemap'),
'#description' => $f->entityCategory == 'instance' ? t('Settings for this specific entity can be overridden here.') : '',
];
// Attach some js magic to forms.
if ($f->entityTypeId != 'comment' || $f->entityCategory != 'instance') // todo: JS not working on comment entity form, hence disabling.
$form['#attached']['library'][] = 'simple_sitemap/form';
// Only attach fieldset summary js to 'additional settings' vertical tabs.
if (isset($form['additional_settings'])) {
$form['#attached']['library'][] = 'simple_sitemap/fieldsetSummaries';
}
$f->displayEntitySettings($form['simple_sitemap']);
$f->displayRegenerateNow($form['simple_sitemap']);
// Add submission handler.
if (isset($form['actions']['submit']['#submit'])) {
......
......@@ -31,36 +31,16 @@ class Form {
* Form constructor.
*/
function __construct($form_state = NULL) {
// Do not alter the form if user lacks certain permissions.
if (!\Drupal::currentUser()->hasPermission('administer sitemap settings')) {
$this->alteringForm = FALSE;
return;
}
$this->formState = $form_state;
$this->entityCategory = NULL;
$this->alteringForm = TRUE;
$this->generator = \Drupal::service('simple_sitemap.generator');
$this->getEntityData();
}
private function getEntityData() {
if (!is_null($this->formState))
if (!is_null($this->formState)) {
$this->getEntityDataFromFormEntity();
// Do not alter the form if it is irrelevant to sitemap generation.
if (empty($this->entityCategory))
$this->alteringForm = FALSE;
// Do not alter the form if entity is not enabled in sitemap settings.
elseif (!$this->generator->entityTypeIsEnabled($this->entityTypeId))
$this->alteringForm = FALSE;
// Do not alter the form, if sitemap is disabled for the entity type of this entity instance.
elseif ($this->entityCategory == 'instance'
&& !$this->generator->bundleIsIndexed($this->entityTypeId, $this->bundleName))
$this->alteringForm = FALSE;
$this->assertAlteringForm();
}
}
public function setEntityCategory($entity_category) {
......@@ -79,7 +59,28 @@ class Form {
$this->instanceId = $instance_id;
}
public function displaySitemapRegenerationSetting(&$form_fragment) {
private function assertAlteringForm() {
// Do not alter the form if user lacks certain permissions.
if (!\Drupal::currentUser()->hasPermission('administer sitemap settings'))
$this->alteringForm = FALSE;
// Do not alter the form if it is irrelevant to sitemap generation.
elseif (empty($this->entityCategory))
$this->alteringForm = FALSE;
// Do not alter the form if entity is not enabled in sitemap settings.
elseif (!$this->generator->entityTypeIsEnabled($this->entityTypeId))
$this->alteringForm = FALSE;
// Do not alter the form, if sitemap is disabled for the entity type of this
// entity instance.
elseif ($this->entityCategory == 'instance'
&& !$this->generator->bundleIsIndexed($this->entityTypeId, $this->bundleName))
$this->alteringForm = FALSE;
}
public function displayRegenerateNow(&$form_fragment) {
$form_fragment['simple_sitemap_regenerate_now'] = [
'#type' => 'checkbox',
'#title' => t('Regenerate sitemap after hitting <em>Save</em>'),
......@@ -91,7 +92,7 @@ class Form {
}
}
public function displayEntitySitemapSettings(&$form_fragment, $multiple = FALSE) {
public function displayEntitySettings(&$form_fragment, $multiple = FALSE) {
$prefix = $multiple ? $this->entityTypeId . '_' : '';
if ($this->entityCategory == 'instance') {
......
......@@ -58,7 +58,7 @@ class SimplesitemapCustomLinksForm extends ConfigFormBase {
];
$f = new Form();
$f->displaySitemapRegenerationSetting($form['simple_sitemap_custom']);
$f->displayRegenerateNow($form['simple_sitemap_custom']);
return parent::buildForm($form, $form_state);
}
......
......@@ -73,11 +73,11 @@ class SimplesitemapEntitiesForm extends ConfigFormBase {
$f->setEntityCategory('bundle');
$f->setEntityTypeId($entity_type_id);
$f->setBundleName($entity_type_id);
$f->displayEntitySitemapSettings($form['simple_sitemap_entities']['entities'][$entity_type_id][$entity_type_id . '_settings'], TRUE);
$f->displayEntitySettings($form['simple_sitemap_entities']['entities'][$entity_type_id][$entity_type_id . '_settings'], TRUE);
$form['#attached']['drupalSettings']['simple_sitemap']['atomic_entities'][] = str_replace('_', '-', $entity_type_id);
}
}
$f->displaySitemapRegenerationSetting($form['simple_sitemap_entities']['entities']);
$f->displayRegenerateNow($form['simple_sitemap_entities']['entities']);
return parent::buildForm($form, $form_state);
}
......
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