Commit 1a75d1bc authored by gbyte.co's avatar gbyte.co

Add some JS magic to sitemap entities form

parent 354ae494
/**
* @file
* Attaches simple_sitemap behaviors to the entity form.
*
* @todo: Tidy up.
*/
(function($) {
"use strict";
// Hide the 'Regenerate sitemap' field to only display it if settings have changed.
$('.form-item-simple-sitemap-regenerate-now').hide();
Drupal.behaviors.simple_sitemapForm = {
attach: function(context) {
// On load.
// Hide the 'Regenerate sitemap' field to only display it if settings have changed.
$('.form-item-simple-sitemap-regenerate-now').hide();
if ($(context).find('#edit-simple-sitemap-index-content-1').is(':checked')) {
// Show 'Priority' field if 'Index sitemap' is ticked.
$('.form-item-simple-sitemap-priority').show();
}
else { // Hide 'Priority' field if 'Index sitemap' is empty.
$('.form-item-simple-sitemap-priority').hide();
}
// On change.
$("#edit-simple-sitemap-index-content").change(function() {
// Show 'Regenerate sitemap' field if setting has changed.
$('.form-item-simple-sitemap-regenerate-now').show();
if ($(context).find('#edit-simple-sitemap-index-content-1').is(':checked')) {
// Show 'Priority' field if 'Index sitemap' is ticked.
$('.form-item-simple-sitemap-priority').show();
}
else { // Hide 'Priority' field if 'Index sitemap' is unticked.
else { // Hide 'Priority' field if 'Index sitemap' is empty.
$('.form-item-simple-sitemap-priority').hide();
}
// Show 'Regenerate sitemap' field if setting has changed.
$( "#edit-simple-sitemap-index-content" ).change(function() {
$('.form-item-simple-sitemap-regenerate-now').show();
if ($(context).find('#edit-simple-sitemap-index-content-1').is(':checked')) {
// Show 'Priority' field if 'Index sitemap' is ticked.
$('.form-item-simple-sitemap-priority').show();
}
else { // Hide 'Priority' field if 'Index sitemap' is unticked.
$('.form-item-simple-sitemap-priority').hide();
}
});
// Show 'Regenerate sitemap' field if setting has changed.
$( "#edit-simple-sitemap-priority" ).change(function() {
$('.form-item-simple-sitemap-regenerate-now').show();
});
});
// Show 'Regenerate sitemap' field if setting has changed.
$("#edit-simple-sitemap-priority").change(function() {
$('.form-item-simple-sitemap-regenerate-now').show();
});
}
};
})(jQuery);
/**
* @file
* Attaches simple_sitemap behaviors to the sitemap entities form.
*/
(function($) {
"use strict";
Drupal.behaviors.simple_sitemapSitemapEntities = {
attach: function(context, settings) {
var allEntities = settings.simple_sitemap.all_entities;
var atomicEntities = settings.simple_sitemap.atomic_entities;
var initiallyChecked = [];
var removingSettingsWarning = Drupal.t("<strong>Warning:</strong> This entity type's sitemap settings including per-entity overrides will be deleted after hitting <em>Save</em>");
// On load.
// Hide the 'Regenerate sitemap' field to only display it if settings have changed.
$('.form-item-simple-sitemap-regenerate-now').hide();
// Get all entities that are enabled on form load.
$.each(allEntities, function(index, value) {
var enabledId = '#edit-' + value + '-enabled';
if ($(context).find(enabledId).is(':checked')) {
initiallyChecked.push(value);
}
});
// Show priority settings if atomic entity enabled on form load.
$.each(atomicEntities, function(index, value) {
var enabledId = '#edit-' + value + '-enabled';
var priorityId = '.form-item-' + value + '-simple-sitemap-priority';
if ($(context).find(enabledId).is(':checked')) {
// Show 'Priority' field if 'Index sitemap' is ticked.
$(priorityId).show();
}
else { // Hide 'Priority' field if 'Index sitemap' is empty.
$(priorityId).hide();
}
});
// On change.
$.each(allEntities, function(index, value) {
var enabledId = '#edit-' + value + '-enabled';
$(enabledId).change(function() {
if ($(context).find(enabledId).is(':checked')) {
$('#warning-' + value).remove();
}
else {
if ($.inArray(value, initiallyChecked) != -1) {
$('.form-item-' + value + '-enabled')
.append("<div id='warning-" + value + "'>"
+ removingSettingsWarning + "</div>");
}
}
// Show 'Regenerate sitemap' field if setting has changed.
$('.form-item-simple-sitemap-regenerate-now').show();
});
});
$.each(atomicEntities, function(index, value) {
var enabledId = '#edit-' + value + '-enabled';
var priorityId = '.form-item-' + value + '-simple-sitemap-priority';
$(enabledId).change(function() {
if ($(context).find(enabledId).is(':checked')) {
// Show 'Priority' field if 'Index sitemap' is ticked.
$(priorityId).show();
}
else { // Hide 'Priority' field if 'Index sitemap' is empty.
$(priorityId).hide();
}
});
// Show 'Regenerate sitemap' field if setting has changed.
$(priorityId).change(function() {
$('.form-item-simple-sitemap-regenerate-now').show();
});
});
}
};
})(jQuery);
......@@ -10,3 +10,10 @@ form:
js/simple_sitemap.form.js: {}
dependencies:
- core/jquery
sitemapEntities:
version: VERSION
js:
js/simple_sitemap.sitemapEntities.js: {}
dependencies:
- core/jquery
- core/drupalSettings
......@@ -44,6 +44,9 @@ class SimplesitemapEntitiesForm extends ConfigFormBase {
'#markup' => '<p>' . t("Simple XML sitemap settings will be added only to entity forms of entity types enabled here. For all entity types featuring bundles (e.g. <em>node</em>) inclusion settings have to be set on their bundle pages (e.g. <em>page</em>). Disabling an entity type on this page will delete its sitemap settings including per-entity overrides.") . '</p>',
);
$form['#attached']['library'][] = 'simple_sitemap/sitemapEntities';
$form['#attached']['drupalSettings']['simple_sitemap'] = ['entities' => [], 'atomic_entities' => []];
$entity_type_labels = [];
foreach (Simplesitemap::getSitemapEntityTypes() as $entity_type_id => $entity_type) {
$entity_type_labels[$entity_type_id] = $entity_type->getLabel() ? : $entity_type_id;
......@@ -62,16 +65,18 @@ class SimplesitemapEntitiesForm extends ConfigFormBase {
];
$form['simple_sitemap_entities']['entities'][$entity_type_id][$entity_type_id . '_enabled'] = [
'#type' => 'checkbox',
'#title' => t('Enable @entity_type_label support', array('@entity_type_label' => strtolower($entity_type_label))),
'#title' => t('Enable @entity_type_label <em>(@entity_type_id)</em> support', array('@entity_type_label' => strtolower($entity_type_label), '@entity_type_id' => $entity_type_id)),
'#description' => t('Sitemap settings for this entity type can be set on its bundle pages and overridden on its entity pages.'),
'#default_value' => $entity_type_enabled,
];
$form['#attached']['drupalSettings']['simple_sitemap']['all_entities'][] = str_replace('_', '-', $entity_type_id);
if (Simplesitemap::entityTypeIsAtomic($entity_type_id)) {
$form['simple_sitemap_entities']['entities'][$entity_type_id][$entity_type_id . '_enabled']['#description'] = t('Sitemap settings for this entity type can be set below and overridden on its entity pages.');
$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);
$form['#attached']['drupalSettings']['simple_sitemap']['atomic_entities'][] = str_replace('_', '-', $entity_type_id);
}
}
$f->displaySitemapRegenerationSetting($form['simple_sitemap_entities']['entities']);
......
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