View source
<?php
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;
class FormHelper {
use StringTranslationTrait;
const PRIORITY_DEFAULT = 0.5;
const PRIORITY_HIGHEST = 10;
const PRIORITY_DIVIDER = 10;
protected $generator;
protected $entityHelper;
protected $currentUser;
protected $formState;
protected $entityCategory;
protected $entityTypeId;
protected $bundleName;
protected $instanceId;
protected static $allowedFormOperations = [
'default',
'edit',
'add',
'register',
];
protected static $changefreqValues = [
'always',
'hourly',
'daily',
'weekly',
'monthly',
'yearly',
'never',
];
protected static $valuesToCheck = [
'simple_sitemap_index_content',
'simple_sitemap_priority',
'simple_sitemap_changefreq',
'simple_sitemap_include_images',
'simple_sitemap_regenerate_now',
];
public function __construct(Simplesitemap $generator, EntityHelper $entityHelper, AccountProxyInterface $current_user) {
$this->generator = $generator;
$this->entityHelper = $entityHelper;
$this->currentUser = $current_user;
}
public function processForm(FormStateInterface $form_state) {
$this->formState = $form_state;
$this
->cleanUpFormInfo();
$this
->getEntityDataFromFormEntity();
return $this
->supports();
}
public function setEntityCategory($entity_category) {
$this->entityCategory = $entity_category;
return $this;
}
public function getEntityCategory() {
return $this->entityCategory;
}
public function setEntityTypeId($entity_type_id) {
$this->entityTypeId = $entity_type_id;
return $this;
}
public function getEntityTypeId() {
return $this->entityTypeId;
}
public function setBundleName($bundle_name) {
$this->bundleName = $bundle_name;
return $this;
}
public function getBundleName() {
return $this->bundleName;
}
public function setInstanceId($instance_id) {
$this->instanceId = $instance_id;
return $this;
}
public function getInstanceId() {
return $this->instanceId;
}
protected function supports() {
if (!$this->currentUser
->hasPermission('administer sitemap settings')) {
return FALSE;
}
elseif (empty($this
->getEntityCategory())) {
return FALSE;
}
elseif (!$this->generator
->entityTypeIsEnabled($this
->getEntityTypeId())) {
return FALSE;
}
elseif ($this
->getEntityCategory() === 'instance' && !$this->generator
->bundleIsIndexed($this
->getEntityTypeId(), $this
->getBundleName())) {
return FALSE;
}
return TRUE;
}
public function displayRegenerateNow(&$form_fragment) {
$form_fragment['simple_sitemap_regenerate_now'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Regenerate sitemap after hitting <em>Save</em>'),
'#description' => $this
->t('This setting will regenerate the whole sitemap including the above changes.'),
'#default_value' => FALSE,
];
if ($this->generator
->getSetting('cron_generate')) {
$form_fragment['simple_sitemap_regenerate_now']['#description'] .= '</br>' . $this
->t('Otherwise the sitemap will be regenerated during a future cron run.');
}
}
public function displayEntitySettings(&$form_fragment, $multiple = FALSE) {
$prefix = $multiple ? $this
->getEntityTypeId() . '_' : '';
if ($this
->getEntityCategory() === 'instance') {
$bundle_settings = $this->generator
->getBundleSettings($this
->getEntityTypeId(), $this
->getBundleName());
$settings = NULL !== $this
->getInstanceId() ? $this->generator
->getEntityInstanceSettings($this
->getEntityTypeId(), $this
->getInstanceId()) : $bundle_settings;
}
else {
$settings = $this->generator
->getBundleSettings($this
->getEntityTypeId(), $this
->getBundleName());
}
Simplesitemap::supplementDefaultSettings('entity', $settings, [
'index' => 0,
]);
$bundle_name = !empty($this
->getBundleName()) ? $this
->getBundleName() : $this
->t('undefined');
if (!$multiple) {
$form_fragment[$prefix . 'simple_sitemap_index_content'] = [
'#type' => 'radios',
'#default_value' => $settings['index'],
'#options' => [
0 => $this
->getEntityCategory() === 'instance' ? $this
->t('Do not index this @bundle entity', [
'@bundle' => $bundle_name,
]) : $this
->t('Do not index entities of this type'),
1 => $this
->getEntityCategory() === 'instance' ? $this
->t('Index this @bundle entity', [
'@bundle' => $bundle_name,
]) : $this
->t('Index entities of this type'),
],
];
if ($this
->getEntityCategory() === 'instance' && isset($bundle_settings['index'])) {
$form_fragment[$prefix . 'simple_sitemap_index_content']['#options'][$bundle_settings['index']] .= ' <em>(' . $this
->t('default') . ')</em>';
}
}
$form_fragment[$prefix . 'simple_sitemap_priority'] = [
'#type' => 'select',
'#title' => $this
->t('Priority'),
'#description' => $this
->getEntityCategory() === 'instance' ? $this
->t('The priority this @bundle entity will have in the eyes of search engine bots.', [
'@bundle' => $bundle_name,
]) : $this
->t('The priority entities of this type will have in the eyes of search engine bots.'),
'#default_value' => $settings['priority'],
'#options' => $this
->getPrioritySelectValues(),
];
if ($this
->getEntityCategory() === 'instance' && isset($bundle_settings['priority'])) {
$form_fragment[$prefix . 'simple_sitemap_priority']['#options'][$this
->formatPriority($bundle_settings['priority'])] .= ' (' . $this
->t('default') . ')';
}
$form_fragment[$prefix . 'simple_sitemap_changefreq'] = [
'#type' => 'select',
'#title' => $this
->t('Change frequency'),
'#description' => $this
->getEntityCategory() === 'instance' ? $this
->t('The frequency with which this @bundle entity changes. Search engine bots may take this as an indication of how often to index it.', [
'@bundle' => $bundle_name,
]) : $this
->t('The frequency with which entities of this type change. Search engine bots may take this as an indication of how often to index them.'),
'#default_value' => $settings['changefreq'],
'#options' => $this
->getChangefreqSelectValues(),
];
if ($this
->getEntityCategory() === 'instance' && isset($bundle_settings['changefreq'])) {
$form_fragment[$prefix . 'simple_sitemap_changefreq']['#options'][$bundle_settings['changefreq']] .= ' (' . $this
->t('default') . ')';
}
$form_fragment[$prefix . 'simple_sitemap_include_images'] = [
'#type' => 'select',
'#title' => $this
->t('Include images'),
'#description' => $this
->getEntityCategory() === 'instance' ? $this
->t('Determines if images referenced by this @bundle entity should be included in the sitemap.', [
'@bundle' => $bundle_name,
]) : $this
->t('Determines if images referenced by entities of this type should be included in the sitemap.'),
'#default_value' => $settings['include_images'],
'#options' => [
0 => $this
->t('No'),
1 => $this
->t('Yes'),
],
];
if ($this
->getEntityCategory() === 'instance' && isset($bundle_settings['include_images'])) {
$form_fragment[$prefix . 'simple_sitemap_include_images']['#options'][$bundle_settings['include_images']] .= ' (' . $this
->t('default') . ')';
}
return $this;
}
protected function getEntityDataFromFormEntity() {
if (!($form_entity = $this
->getFormEntity())) {
return FALSE;
}
$entity_type_id = $form_entity
->getEntityTypeId();
$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_type) {
if ($sitemap_entity_type
->getBundleEntityType() === $entity_type_id) {
$this
->setEntityCategory('bundle');
break;
}
}
}
$this
->setEntityCategory(NULL === $this
->getEntityCategory() && $entity_type_id === 'menu' ? 'bundle' : $this
->getEntityCategory());
switch ($this
->getEntityCategory()) {
case 'bundle':
$this
->setEntityTypeId($this->entityHelper
->getBundleEntityTypeId($form_entity));
$this
->setBundleName($form_entity
->id());
$this
->setInstanceId(NULL);
break;
case 'instance':
$this
->setEntityTypeId($entity_type_id);
$this
->setBundleName($this->entityHelper
->getEntityInstanceBundleName($form_entity));
$this
->setInstanceId(!empty($form_entity
->id()) ? $form_entity
->id() : NULL);
break;
default:
return FALSE;
}
return TRUE;
}
protected function getFormEntity() {
$form_object = $this->formState
->getFormObject();
if (NULL !== $form_object && method_exists($form_object, 'getOperation') && method_exists($form_object, 'getEntity') && in_array($form_object
->getOperation(), self::$allowedFormOperations)) {
return $form_object
->getEntity();
}
return FALSE;
}
protected function cleanUpFormInfo() {
$this->entityCategory = NULL;
$this->entityTypeId = NULL;
$this->bundleName = NULL;
$this->instanceId = NULL;
}
public function getFormEntityId() {
return $this->formState
->getFormObject()
->getEntity()
->id();
}
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;
}
}
return FALSE;
}
public function getPrioritySelectValues() {
$options = [];
foreach (range(0, self::PRIORITY_HIGHEST) as $value) {
$value = $this
->formatPriority($value / self::PRIORITY_DIVIDER);
$options[$value] = $value;
}
return $options;
}
public function getChangefreqSelectValues() {
$options = [
'' => $this
->t('- Not specified -'),
];
foreach (self::$changefreqValues as $setting) {
$options[$setting] = $this
->t($setting);
}
return $options;
}
public static function getChangefreqOptions() {
return self::$changefreqValues;
}
public function formatPriority($priority) {
return number_format((double) $priority, 1, '.', '');
}
public static function isValidPriority($priority) {
return is_numeric($priority) && $priority >= 0 && $priority <= 1;
}
public static function isValidChangefreq($changefreq) {
return in_array($changefreq, self::$changefreqValues);
}
}