Form.php 9.08 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php
/**
 * @file
 * Contains \Drupal\simple_sitemap\Form.
 */

namespace Drupal\simple_sitemap;

/**
 * Form class.
 */
class Form {

gbyte.co's avatar
gbyte.co committed
14 15 16 17
  const PRIORITY_DEFAULT = 0.5;
  const PRIORITY_HIGHEST = 10;
  const PRIORITY_DIVIDER = 10;

18 19
  public $alteringForm;

20
  public $entityCategory;
21 22
  public $entityTypeId;
  public $bundleName;
23
  public $instanceId;
24
  
25
  private $formState;
26 27
  
  private $sitemap;
28 29 30 31

  /**
   * Form constructor.
   */
32
  function __construct($form_state = NULL) {
33
    $this->formState = $form_state;
34
    $this->entityCategory = NULL;
35 36 37 38 39 40 41 42 43 44
    $this->alteringForm = TRUE;
    $this->sitemap = \Drupal::service('simple_sitemap.generator');

    // Do not alter the form if user lacks certain permissions.
    if (!\Drupal::currentUser()->hasPermission('administer sitemap settings')) {
      $this->alteringForm = FALSE;
      return;
    }
    $this->getEntityData();
  }
45

46 47
  private function getEntityData() {
    if (!is_null($this->formState))
48
      $this->getEntityDataFromFormEntity();
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

    $entity_types = $this->sitemap->getConfig('entity_types');

    // 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 (!isset($entity_types[$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' && empty($entity_types[$this->entityTypeId][$this->bundleName]['index']))
      $this->alteringForm = FALSE;
  }

  public function setEntityCategory($entity_category) {
    $this->entityCategory = $entity_category;
  }

  public function setEntityTypeId($entity_type_id) {
    $this->entityTypeId = $entity_type_id;
  }

  public function setBundleName($bundle_name) {
    $this->bundleName = $bundle_name;
  }

  public function setInstanceId($instance_id) {
    $this->instanceId = $instance_id;
  }

  public function displaySitemapRegenerationSetting(&$form_fragment) {
    $form_fragment['simple_sitemap_regenerate_now'] = array(
      '#type' => 'checkbox',
      '#title' => t('Regenerate sitemap after hitting <em>Save</em>'),
      '#description' => t('This setting will regenerate the whole sitemap including the above changes.'),
      '#default_value' => FALSE,
    );
    if ($this->sitemap->getSetting('cron_generate')) {
      $form_fragment['simple_sitemap_regenerate_now']['#description'] .= '</br>' . t('Otherwise the sitemap will be regenerated on the next cron run.');
90 91
    }
  }
92 93 94
  
  public function displayEntitySitemapSettings(&$form_fragment, $multiple = FALSE) {
    $prefix = $multiple ? $this->entityTypeId . '_' : '';
95

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    // Setting default form values.
    $index = 0;
    $priority = self::PRIORITY_DEFAULT;
    $entity_types = $this->sitemap->getConfig('entity_types');

    // Overwriting defaults if settings found for bundle.
    if (isset($entity_types[$this->entityTypeId][$this->bundleName]['index']))
      $bundle_index = $index = $entity_types[$this->entityTypeId][$this->bundleName]['index'];
    if (isset($entity_types[$this->entityTypeId][$this->bundleName]['priority']))
      $bundle_priority = $priority = $entity_types[$this->entityTypeId][$this->bundleName]['priority'];

    // Overwriting defaults if settings found for this entity instance.
    if ($this->entityCategory == 'instance') {
      if (isset($entity_types[$this->entityTypeId][$this->bundleName]['entities'][$this->instanceId]['index']))
        $index = $entity_types[$this->entityTypeId][$this->bundleName]['entities'][$this->instanceId]['index'];
      if (isset($entity_types[$this->entityTypeId][$this->bundleName]['entities'][$this->instanceId]['priority']))
        $priority = $entity_types[$this->entityTypeId][$this->bundleName]['entities'][$this->instanceId]['priority'];
    }

    if (!$multiple) {
      $form_fragment[$prefix . 'simple_sitemap_index_content'] = [
        '#type' => 'radios',
        '#default_value' => $index,
        '#options' => [
          0 => $this->entityCategory == 'instance' ? t('Do not index this entity') : t('Do not index entities of this type'),
          1 => $this->entityCategory == 'instance' ? t('Index this entity') : t('Index entities of this type'),
        ]
      ];
      if ($this->entityCategory == 'instance' && isset($bundle_index)) {
        $form_fragment[$prefix . 'simple_sitemap_index_content']['#options'][$bundle_index] .= ' <em>(' . t('Default') . ')</em>';
      }
    }

    if ($this->entityCategory == 'instance') {
      $priority_description = t('The priority this entity will have in the eyes of search engine bots.');
    }
    elseif (!$multiple) {
      $priority_description = t('The priority entities of this bundle will have in the eyes of search engine bots.');
    }
    else {
      $priority_description = t('The priority entities of this type will have in the eyes of search engine bots.');
    }
    $form_fragment[$prefix . 'simple_sitemap_priority'] = [
      '#type' => 'select',
      '#title' => t('Priority'),
      '#description' => $priority_description,
      '#default_value' => $priority,
      '#options' => self::getPrioritySelectValues(),
    ];
    if ($this->entityCategory == 'instance' && isset($bundle_priority)) {
      $form_fragment[$prefix . 'simple_sitemap_priority']['#options'][(string)$bundle_priority] .= ' (' . t('Default') . ')';
147 148 149 150 151 152 153 154 155 156
    }
  }

  /**
   * Checks if this particular form is a bundle form, or a bundle instance form
   * and gathers sitemap settings from the database.
   *
   * @return bool
   *  TRUE if this is a bundle or bundle instance form, FALSE otherwise.
   */
157
  private function getEntityDataFromFormEntity() {
158 159
    $form_entity = $this->getFormEntity();
    if ($form_entity !== FALSE) {
160
      $entity_type_id = $form_entity->getEntityTypeId();
161 162 163 164
      $sitemap_entity_types = Simplesitemap::getSitemapEntityTypes();
      $entity_bundle = $form_entity->bundle();
      if (isset($sitemap_entity_types[$entity_type_id])) {
        $this->entityCategory = 'instance';
165 166
      }
      else {
167 168 169 170
        foreach ($sitemap_entity_types as $sitemap_entity) {
          if ($sitemap_entity->getBundleEntityType() == $entity_type_id) {
            $this->entityCategory = 'bundle';
            break;
171 172 173
          }
        }
      }
174

175
      // Menu fix.
176 177
      if (is_null($this->entityCategory) && $entity_type_id == 'menu') {
        $this->entityCategory = 'bundle';
178
        $entity_type_id = 'menu_link_content';
179 180 181 182
      }

      switch ($this->entityCategory) {
        case 'bundle':
183
          $this->entityTypeId = $entity_type_id == 'menu_link_content' ? $entity_type_id : $form_entity->getEntityType()->getBundleOf(); // Menu fix.
184 185 186 187 188
          $this->bundleName = $form_entity->id();
          $this->instanceId = NULL;
          break;

        case 'instance':
189 190
          $this->entityTypeId = $entity_type_id;
          $this->bundleName = $entity_bundle == 'menu_link_content' && method_exists($form_entity, 'getMenuName') ? $form_entity->getMenuName() : $entity_bundle; // Menu fix.
191 192 193 194 195 196 197
          $this->instanceId = $form_entity->id();
          break;

        default:
          return FALSE;
      }
      return TRUE;
198 199 200 201 202 203 204 205 206 207 208 209 210 211
    }
    return FALSE;
  }

  /**
   * Gets the object entity of the form if available.
   *
   * @return object $entity or FALSE if non-existent or if form operation is
   *  'delete'.
   */
  private function getFormEntity() {
    $form_object = $this->formState->getFormObject();
    if (!is_null($form_object)
      && method_exists($form_object, 'getEntity')
212
      && in_array($form_object->getOperation(), ['default', 'edit'])) {
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
      return $form_object->getEntity();
    }
    return FALSE;
  }

  /**
   * Gets new entity Id after entity creation.
   * To be used in an entity form submit.
   *
   * @return int entity ID.
   */
  public static function getNewEntityId($form_state) {
    return $form_state->getFormObject()->getEntity()->id();
  }

  /**
   * Checks if simple_sitemap values have been changed after submitting the form.
   * To be used in an entity form submit.
   *
   * @return bool
   *  TRUE if simple_sitemap form values have been altered by the user.
   */
  public static function valuesChanged($form, $form_state) {
    $values = $form_state->getValues();
    foreach (array('simple_sitemap_index_content', 'simple_sitemap_priority', 'simple_sitemap_regenerate_now') as $field_name) {
238 239
      if (isset($values['simple_sitemap'][$field_name]) && $values['simple_sitemap'][$field_name] != $form['simple_sitemap'][$field_name]['#default_value']
        || isset($values[$field_name]) && $values[$field_name] != $form['simple_sitemap'][$field_name]['#default_value']) { // Fix for values appearing in a sub array on a commerce product entity.
240 241 242 243 244
        return TRUE;
      }
    }
    return FALSE;
  }
gbyte.co's avatar
gbyte.co committed
245 246 247 248 249 250 251 252 253 254 255 256 257 258

  /**
   * Gets the values needed to display the priority dropdown setting.
   *
   * @return array $options
   */
  public static function getPrioritySelectValues() {
    $options = array();
    foreach(range(0, self::PRIORITY_HIGHEST) as $value) {
      $value = $value / self::PRIORITY_DIVIDER;
      $options[(string)$value] = (string)$value;
    }
    return $options;
  }
259
}