"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.10/core/modules/system/src/Form/DateFormatFormBase.php" (26 Nov 2020, 5167 Bytes) of package /linux/www/drupal-8.9.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "DateFormatFormBase.php" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 9.0.8_vs_9.1.0-rc1.

    1 <?php
    2 
    3 namespace Drupal\system\Form;
    4 
    5 use Drupal\Core\Config\Entity\ConfigEntityStorageInterface;
    6 use Drupal\Core\Datetime\DateFormatterInterface;
    7 use Drupal\Core\Form\FormStateInterface;
    8 use Drupal\Core\Language\LanguageInterface;
    9 use Symfony\Component\DependencyInjection\ContainerInterface;
   10 use Drupal\Core\Entity\EntityForm;
   11 
   12 /**
   13  * Provides a base form for date formats.
   14  */
   15 abstract class DateFormatFormBase extends EntityForm {
   16 
   17   /**
   18    * The date formatter service.
   19    *
   20    * @var \Drupal\Core\Datetime\DateFormatterInterface
   21    */
   22   protected $dateFormatter;
   23 
   24   /**
   25    * The date format storage.
   26    *
   27    * @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
   28    */
   29   protected $dateFormatStorage;
   30 
   31   /**
   32    * Constructs a new date format form.
   33    *
   34    * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
   35    *   The date service.
   36    * @param \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $date_format_storage
   37    *   The date format storage.
   38    */
   39   public function __construct(DateFormatterInterface $date_formatter, ConfigEntityStorageInterface $date_format_storage) {
   40     $this->dateFormatter = $date_formatter;
   41     $this->dateFormatStorage = $date_format_storage;
   42   }
   43 
   44   /**
   45    * {@inheritdoc}
   46    */
   47   public static function create(ContainerInterface $container) {
   48     return new static(
   49       $container->get('date.formatter'),
   50       $container->get('entity_type.manager')->getStorage('date_format')
   51     );
   52   }
   53 
   54   /**
   55    * Checks for an existing date format.
   56    *
   57    * @param string|int $entity_id
   58    *   The entity ID.
   59    * @param array $element
   60    *   The form element.
   61    *
   62    * @return bool
   63    *   TRUE if this format already exists, FALSE otherwise.
   64    */
   65   public function exists($entity_id, array $element) {
   66     return (bool) $this->dateFormatStorage
   67       ->getQuery()
   68       ->condition('id', $element['#field_prefix'] . $entity_id)
   69       ->execute();
   70   }
   71 
   72   /**
   73    * {@inheritdoc}
   74    */
   75   public function form(array $form, FormStateInterface $form_state) {
   76     $form['label'] = [
   77       '#type' => 'textfield',
   78       '#title' => 'Name',
   79       '#maxlength' => 100,
   80       '#description' => t('Name of the date format'),
   81       '#default_value' => $this->entity->label(),
   82     ];
   83 
   84     $form['id'] = [
   85       '#type' => 'machine_name',
   86       '#description' => t('A unique machine-readable name. Can only contain lowercase letters, numbers, and underscores.'),
   87       '#disabled' => !$this->entity->isNew(),
   88       '#default_value' => $this->entity->id(),
   89       '#machine_name' => [
   90         'exists' => [$this, 'exists'],
   91         'replace_pattern' => '([^a-z0-9_]+)|(^custom$)',
   92         'error' => $this->t('The machine-readable name must be unique, and can only contain lowercase letters, numbers, and underscores. Additionally, it can not be the reserved word "custom".'),
   93       ],
   94     ];
   95     $form['date_format_pattern'] = [
   96       '#type' => 'textfield',
   97       '#title' => t('Format string'),
   98       '#maxlength' => 100,
   99       '#description' => $this->t('A user-defined date format. See the <a href="http://php.net/manual/function.date.php">PHP manual</a> for available options.'),
  100       '#required' => TRUE,
  101       '#attributes' => [
  102         'data-drupal-date-formatter' => 'source',
  103       ],
  104       '#field_suffix' => ' <small class="js-hide" data-drupal-date-formatter="preview">' . $this->t('Displayed as %date_format', ['%date_format' => '']) . '</small>',
  105     ];
  106 
  107     $form['langcode'] = [
  108       '#type' => 'language_select',
  109       '#title' => t('Language'),
  110       '#languages' => LanguageInterface::STATE_ALL,
  111       '#default_value' => $this->entity->language()->getId(),
  112     ];
  113     $form['#attached']['drupalSettings']['dateFormats'] = $this->dateFormatter->getSampleDateFormats();
  114     $form['#attached']['library'][] = 'system/drupal.system.date';
  115     return parent::form($form, $form_state);
  116   }
  117 
  118   /**
  119    * {@inheritdoc}
  120    */
  121   public function validateForm(array &$form, FormStateInterface $form_state) {
  122     parent::validateForm($form, $form_state);
  123 
  124     // The machine name field should already check to see if the requested
  125     // machine name is available.
  126     $pattern = trim($form_state->getValue('date_format_pattern'));
  127     foreach ($this->dateFormatStorage->loadMultiple() as $format) {
  128       if ($format->getPattern() == $pattern && ($format->id() == $this->entity->id())) {
  129         $this->messenger()->addStatus($this->t('The existing format/name combination has not been altered.'));
  130         continue;
  131       }
  132     }
  133   }
  134 
  135   /**
  136    * {@inheritdoc}
  137    */
  138   public function submitForm(array &$form, FormStateInterface $form_state) {
  139     $form_state->setValue('pattern', trim($form_state->getValue('date_format_pattern')));
  140     parent::submitForm($form, $form_state);
  141   }
  142 
  143   /**
  144    * {@inheritdoc}
  145    */
  146   public function save(array $form, FormStateInterface $form_state) {
  147     $status = $this->entity->save();
  148     if ($status == SAVED_UPDATED) {
  149       $this->messenger()->addStatus($this->t('Custom date format updated.'));
  150     }
  151     else {
  152       $this->messenger()->addStatus($this->t('Custom date format added.'));
  153     }
  154     $form_state->setRedirectUrl($this->entity->toUrl('collection'));
  155   }
  156 
  157 }