Commit 9830e2d9 authored by gbyte.co's avatar gbyte.co

Multiple variants UI WIP

parent d124a9ed
......@@ -60,15 +60,18 @@ class FormHelper {
protected $instanceId;
/**
* @var string
* @var array
*/
protected $variant;
protected $bundleSettings;
/**
* @var array
*/
protected $bundleSettings;
protected $instanceSettings;
/**
* @var array
*/
protected static $allowedFormOperations = [
'default',
'edit',
......@@ -76,6 +79,9 @@ class FormHelper {
'register',
];
/**
* @var array
*/
protected static $changefreqValues = [
'always',
'hourly',
......@@ -86,6 +92,9 @@ class FormHelper {
'never',
];
/**
* @var array
*/
protected static $valuesToCheck = [
'simple_sitemap_variant',
'simple_sitemap_index_content',
......@@ -119,7 +128,7 @@ class FormHelper {
$this->formState = $form_state;
$this->cleanUpFormInfo();
$this->getEntityDataFromFormEntity();
$this->negotiateVariant();
$this->negotiateSettings();
return $this->supports();
}
......@@ -146,6 +155,7 @@ class FormHelper {
*/
public function setEntityTypeId($entity_type_id) {
$this->entityTypeId = $entity_type_id;
return $this;
}
......@@ -162,6 +172,7 @@ class FormHelper {
*/
public function setBundleName($bundle_name) {
$this->bundleName = $bundle_name;
return $this;
}
......@@ -178,6 +189,7 @@ class FormHelper {
*/
public function setInstanceId($instance_id) {
$this->instanceId = $instance_id;
return $this;
}
......@@ -208,11 +220,11 @@ class FormHelper {
return FALSE;
}
// Do not alter the form, if sitemap is disabled for the entity type of this
// entity instance.
// Do not alter the form, if sitemap is disabled for the entity type or
// bundle of this entity instance.
elseif ($this->getEntityCategory() === 'instance') {
if (NULL === $this->variant || !$this->generator
->setVariants($this->variant)
if (NULL === $this->instanceSettings || !$this->generator
->setVariants(TRUE)
->bundleIsIndexed($this->getEntityTypeId(), $this->getBundleName())) {
return FALSE;
}
......@@ -241,34 +253,47 @@ class FormHelper {
/**
* @return $this
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public function negotiateVariant() {
$all_bundle_settings = $this->generator->setVariants(TRUE)
public function negotiateSettings() {
$this->bundleSettings = $this->generator->setVariants(TRUE)
->getBundleSettings($this->getEntityTypeId(), $this->getBundleName(), FALSE, TRUE);
$this->bundleSettings = NULL !== ($variant = key($all_bundle_settings))
? $all_bundle_settings[$variant]
: [];
$this->variant = $variant;
if ($this->getEntityCategory() === 'instance' && NULL !== $this->getInstanceId()) {
$this->instanceSettings = $this->generator->setVariants(TRUE)->getEntityInstanceSettings($this->getEntityTypeId(), $this->getInstanceId());
}
return $this;
}
/**
* @param array $form_fragment
* @param $form_fragment
* @param bool $multiple
* @return $this
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public function displayEntitySettings(&$form_fragment, $multiple = FALSE) {
$prefix = $multiple ? $this->getEntityTypeId() . '_' : '';
$settings = $this->getEntityCategory() === 'instance' && NULL !== $this->variant && NULL !== $this->getInstanceId()
? $this->generator->setVariants($this->variant)->getEntityInstanceSettings($this->getEntityTypeId(), $this->getInstanceId())
: $this->bundleSettings;
$bundle_name = !empty($this->getBundleName()) ? $this->getBundleName() : $this->t('undefined');
$settings = $this->getEntityCategory() === 'instance' ? $this->instanceSettings : $this->bundleSettings;
Simplesitemap::supplementDefaultSettings('entity', $settings);
$bundle_name = !empty($this->getBundleName()) ? $this->getBundleName() : $this->t('undefined');
// Variants
$variants = array_keys($settings); // $variants = $this->generator->getSitemapManager()->getSitemapVariants(NULL, FALSE);
$indexed_variants = [];
foreach ($variants as $variant) {
if (!empty($settings[$variant]['index'])) {
$indexed_variants[] = $variant;
}
}
$form_fragment[$prefix . 'simple_sitemap_variant'] = [
'#type' => 'select',
'#title' => $this->t('Sitemap variants'),
'#description' => $this->t('The sitemap variants entities of this type are to be indexed in.'),
'#options' => array_map(function($variants) { return $this->t($variants['label']); }, $variants),
'#disabled' => $this->getEntityCategory() === 'instance',
'#default_value' => $indexed_variants,
];
// Index
if (!$multiple) {
......@@ -290,37 +315,6 @@ class FormHelper {
}
}
// Variant
$form_fragment[$prefix . 'simple_sitemap_variant'] = [
'#type' => 'select',
'#title' => $this->t('Sitemap variant'),
'#description' => $this->t('The sitemap variant entities of this type are to be indexed in.'),
'#options' => array_map(
function($variant) { return $this->t($variant['label']); },
$this->generator->getSitemapManager()->getSitemapVariants(NULL, FALSE)
),
'#states' => [
'visible' => !$multiple
? [':input[name="' . $prefix . 'simple_sitemap_index_content"]' => ['value' => 1]]
: [':input[name="' . $prefix . 'enabled"]' => ['checked' => TRUE]],
'required' => !$multiple // todo Should implement server side validation on top of this.
? [':input[name="' . $prefix . 'simple_sitemap_index_content"]' => ['value' => 1]]
: [':input[name="' . $prefix . 'enabled"]' => ['checked' => TRUE]],
],
'#disabled' => $this->getEntityCategory() === 'instance'
];
// If only one variant is available, set it, otherwise check if a default
// variant is provided and set it.
$form_fragment[$prefix . 'simple_sitemap_variant']['#default_value'] = NULL === $this->variant
? (1 === count($form_fragment[$prefix . 'simple_sitemap_variant']['#options'])
? array_keys($form_fragment[$prefix . 'simple_sitemap_variant']['#options'])[0]
: (!empty($default = $this->generator->getSetting('default_variant'))
? $default
: $this->variant
)
)
: $this->variant;
// Priority
$form_fragment[$prefix . 'simple_sitemap_priority'] = [
'#type' => 'select',
......@@ -465,8 +459,8 @@ class FormHelper {
$this->entityTypeId = NULL;
$this->bundleName = NULL;
$this->instanceId = NULL;
$this->variant = NULL;
$this->bundleSettings = NULL;
$this->instanceSettings = NULL;
return $this;
}
......
......@@ -136,7 +136,7 @@ class SimplesitemapEntitiesForm extends SimplesitemapFormBase {
->setEntityCategory('bundle')
->setEntityTypeId($entity_type_id)
->setBundleName($entity_type_id)
->negotiateVariant()
->negotiateSettings()
->displayEntitySettings(
$form['simple_sitemap_entities']['entities'][$entity_type_id][$entity_type_id . '_settings'],
TRUE
......
......@@ -781,19 +781,23 @@ class Simplesitemap {
/**
* Checks if an entity bundle (or a non-bundle entity type) is set to be
* indexed for the currently set variant.
* indexed for any of the currently set variants.
*
* @param string $entity_type_id
* @param string|null $bundle_name
*
* @return bool
*
* @todo multiple variants?
* @todo Test.
*/
public function bundleIsIndexed($entity_type_id, $bundle_name = NULL) {
$settings = $this->getBundleSettings($entity_type_id, $bundle_name);
foreach ($this->getBundleSettings($entity_type_id, $bundle_name, FALSE, TRUE) as $variant_settings) {
if (!empty($settings['index'])) {
return TRUE;
}
}
return !empty($settings['index']);
return FALSE;
}
/**
......
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