3 namespace Drupal\simple_sitemap\Form;
5 use Drupal\Core\StringTranslation\StringTranslationTrait;
6 use Drupal\simple_sitemap\EntityHelper;
7 use Drupal\simple_sitemap\Simplesitemap;
8 use Drupal\Core\Session\AccountProxyInterface;
9 use Drupal\Core\Form\FormState;
13 * @package Drupal\simple_sitemap\Form
16 use StringTranslationTrait;
18 const PRIORITY_DEFAULT = 0.5;
19 const PRIORITY_HIGHEST = 10;
20 const PRIORITY_DIVIDER = 10;
23 * @var \Drupal\simple_sitemap\Simplesitemap
28 * @var \Drupal\simple_sitemap\EntityHelper
30 protected $entityHelper;
33 * @var \Drupal\Core\Session\AccountProxyInterface
35 protected $currentUser;
38 * @var \Drupal\Core\Form\FormState
45 protected $alteringForm = TRUE;
50 protected $entityCategory = NULL;
55 protected $entityTypeId;
60 protected $bundleName;
65 protected $instanceId;
67 protected static $allowedFormOperations = [
74 protected static $valuesToCheck = [
75 'simple_sitemap_index_content',
76 'simple_sitemap_priority',
77 'simple_sitemap_regenerate_now',
81 * FormHelper constructor.
82 * @param \Drupal\simple_sitemap\Simplesitemap $generator
83 * @param \Drupal\simple_sitemap\EntityHelper $entityHelper
84 * @param \Drupal\Core\Session\AccountProxyInterface $current_user
86 public function __construct(
87 Simplesitemap $generator,
88 EntityHelper $entityHelper,
89 AccountProxyInterface $current_user
91 $this->generator = $generator;
92 $this->entityHelper = $entityHelper;
93 $this->currentUser = $current_user;
97 * @param \Drupal\Core\Form\FormState $form_state
100 public function processForm(FormState $form_state) {
101 $this->formState = $form_state;
102 $this->getEntityDataFromFormEntity();
103 $this->assertAlteringForm();
110 public function alteringForm() {
111 return $this->alteringForm;
115 * @param string $entity_category
118 public function setEntityCategory($entity_category) {
119 $this->entityCategory = $entity_category;
124 * @return null|string
126 public function getEntityCategory() {
127 return $this->entityCategory;
131 * @param string $entity_type_id
134 public function setEntityTypeId($entity_type_id) {
135 $this->entityTypeId = $entity_type_id;
142 public function getEntityTypeId() {
143 return $this->entityTypeId;
147 * @param string $bundle_name
150 public function setBundleName($bundle_name) {
151 $this->bundleName = $bundle_name;
158 public function getBundleName() {
159 return $this->bundleName;
163 * @param string $instance_id
166 public function setInstanceId($instance_id) {
167 $this->instanceId = $instance_id;
174 public function getInstanceId() {
175 return $this->instanceId;
181 protected function assertAlteringForm() {
183 // Do not alter the form if user lacks certain permissions.
184 if (!$this->currentUser->hasPermission('administer sitemap settings')) {
185 $this->alteringForm = FALSE;
188 // Do not alter the form if it is irrelevant to sitemap generation.
189 elseif (empty($this->getEntityCategory())) {
190 $this->alteringForm = FALSE;
193 // Do not alter the form if entity is not enabled in sitemap settings.
194 elseif (!$this->generator->entityTypeIsEnabled($this->getEntityTypeId())) {
195 $this->alteringForm = FALSE;
198 // Do not alter the form, if sitemap is disabled for the entity type of this
200 elseif ($this->getEntityCategory() == 'instance'
201 && !$this->generator->bundleIsIndexed($this->getEntityTypeId(), $this->getBundleName())) {
202 $this->alteringForm = FALSE;
207 * @param array $form_fragment
209 public function displayRegenerateNow(&$form_fragment) {
210 $form_fragment['simple_sitemap_regenerate_now'] = [
211 '#type' => 'checkbox',
212 '#title' => $this->t('Regenerate sitemap after hitting <em>Save</em>'),
213 '#description' => $this->t('This setting will regenerate the whole sitemap including the above changes.'),
214 '#default_value' => FALSE,
216 if ($this->generator->getSetting('cron_generate')) {
217 $form_fragment['simple_sitemap_regenerate_now']['#description'] .= '</br>' . $this->t('Otherwise the sitemap will be regenerated on the next cron run.');
222 * @param array $form_fragment
223 * @param bool $multiple
226 public function displayEntitySettings(&$form_fragment, $multiple = FALSE) {
227 $prefix = $multiple ? $this->getEntityTypeId() . '_' : '';
229 if ($this->getEntityCategory() == 'instance') {
230 $bundle_settings = $this->generator->getBundleSettings($this->getEntityTypeId(), $this->getBundleName());
231 $settings = NULL !== $this->getInstanceId() ? $this->generator->getEntityInstanceSettings($this->getEntityTypeId(), $this->getInstanceId()) : $bundle_settings;
234 $settings = $this->generator->getBundleSettings($this->getEntityTypeId(), $this->getBundleName());
236 $index = isset($settings['index']) ? $settings['index'] : 0;
237 $priority = isset($settings['priority']) ? $settings['priority'] : self::PRIORITY_DEFAULT;
238 $bundle_name = !empty($this->getBundleName()) ? $this->getBundleName() : $this->t('undefined');
241 $form_fragment[$prefix . 'simple_sitemap_index_content'] = [
243 '#default_value' => $index,
245 0 => $this->getEntityCategory() == 'instance' ? $this->t('Do not index this @bundle entity', ['@bundle' => $bundle_name]) : $this->t('Do not index entities of this type'),
246 1 => $this->getEntityCategory() == 'instance' ? $this->t('Index this @bundle entity', ['@bundle' => $bundle_name]) : $this->t('Index entities of this type'),
249 if ($this->getEntityCategory() == 'instance' && isset($bundle_settings['index'])) {
250 $form_fragment[$prefix . 'simple_sitemap_index_content']['#options'][$bundle_settings['index']] .= ' <em>(' . $this->t('Default') . ')</em>';
254 if ($this->getEntityCategory() == 'instance') {
255 $priority_description = $this->t('The priority this @bundle entity will have in the eyes of search engine bots.', ['@bundle' => $bundle_name]);
258 $priority_description = $this->t('The priority entities of this type will have in the eyes of search engine bots.');
260 $form_fragment[$prefix . 'simple_sitemap_priority'] = [
262 '#title' => $this->t('Priority'),
263 '#description' => $priority_description,
264 '#default_value' => $priority,
265 '#options' => $this->getPrioritySelectValues(),
267 if ($this->getEntityCategory() == 'instance' && isset($bundle_settings['priority'])) {
268 $form_fragment[$prefix . 'simple_sitemap_priority']['#options'][$this->formatPriority($bundle_settings['priority'])] .= ' (' . $this->t('Default') . ')';
274 * Checks if this particular form is a bundle form, or a bundle instance form
275 * and gathers sitemap settings from the database.
278 * TRUE if this is a bundle or bundle instance form, FALSE otherwise.
280 protected function getEntityDataFromFormEntity() {
281 $form_entity = $this->getFormEntity();
282 if ($form_entity !== FALSE) {
283 $entity_type_id = $form_entity->getEntityTypeId();
284 $sitemap_entity_types = $this->entityHelper->getSitemapEntityTypes();
285 if (isset($sitemap_entity_types[$entity_type_id])) {
286 $this->setEntityCategory('instance');
289 foreach ($sitemap_entity_types as $sitemap_entity) {
290 if ($sitemap_entity->getBundleEntityType() == $entity_type_id) {
291 $this->setEntityCategory('bundle');
298 $this->setEntityCategory(NULL === $this->getEntityCategory() && $entity_type_id == 'menu' ? 'bundle' : $this->getEntityCategory());
300 switch ($this->getEntityCategory()) {
302 $this->setEntityTypeId($this->entityHelper->getBundleEntityTypeId($form_entity));
303 $this->setBundleName($form_entity->id());
304 $this->setInstanceId(NULL);
308 $this->setEntityTypeId($entity_type_id);
309 $this->setBundleName($this->entityHelper->getEntityInstanceBundleName($form_entity));
310 // New menu link's id is '' instead of NULL, hence checking for empty.
311 $this->setInstanceId(!empty($form_entity->id()) ? $form_entity->id() : NULL);
323 * Gets the object entity of the form if available.
325 * @return object|false
326 * Entity or FALSE if non-existent or if form operation is
329 protected function getFormEntity() {
330 $form_object = $this->formState->getFormObject();
331 if (NULL !== $form_object
332 && method_exists($form_object, 'getEntity')
333 && in_array($form_object->getOperation(), self::$allowedFormOperations)) {
334 return $form_object->getEntity();
340 * Gets new entity Id after entity creation.
341 * To be used in an entity form submit.
346 public function getFormEntityId() {
347 return $this->formState->getFormObject()->getEntity()->id();
351 * Checks if simple_sitemap values have been changed after submitting the form.
352 * To be used in an entity form submit.
355 * @param array $values
358 * TRUE if simple_sitemap form values have been altered by the user.
360 public function valuesChanged($form, array $values) {
361 foreach (self::$valuesToCheck as $field_name) {
362 if (isset($values[$field_name]) && $values[$field_name] != $form['simple_sitemap'][$field_name]['#default_value']) {
370 * Gets the values needed to display the priority dropdown setting.
374 public function getPrioritySelectValues() {
376 foreach (range(0, self::PRIORITY_HIGHEST) as $value) {
377 $value = $this->formatPriority($value / self::PRIORITY_DIVIDER);
378 $options[$value] = $value;
384 * @param string $priority
387 public function formatPriority($priority) {
388 return number_format((float) $priority, 1, '.', '');
392 * @param string|int $priority
395 public static function isValidPriority($priority) {
396 return is_numeric($priority) && $priority >= 0 && $priority <= 1;