DateFormat.php 3.09 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
<?php

/**
 * @file
 * Contains \Drupal\config_translation\FormElement\DateFormat.
 */

namespace Drupal\config_translation\FormElement;

use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\ReplaceCommand;
13
use Drupal\Core\Form\FormStateInterface;
14
use Drupal\Core\Language\LanguageInterface;
15
use Drupal\Core\StringTranslation\StringTranslationTrait;
16
use Drupal\Core\TypedData\DataDefinitionInterface;
17 18 19 20

/**
 * Defines the date format element for the configuration translation interface.
 */
21 22
class DateFormat implements ElementInterface {
  use StringTranslationTrait;
23 24 25 26

  /**
   * {@inheritdoc}
   */
27
  public function getFormElement(DataDefinitionInterface $definition, LanguageInterface $language, $value) {
28
    $description = $this->t('A user-defined date format. See the <a href="@url">PHP manual</a> for available options.', array('@url' => 'http://php.net/manual/function.date.php'));
29
    $format = $this->t('Displayed as %date_format', array('%date_format' => \Drupal::service('date.formatter')->format(REQUEST_TIME, 'custom', $value)));
30 31
    return array(
      '#type' => 'textfield',
32
      '#title' => $this->t($definition->getLabel()) . '<span class="visually-hidden"> (' . $language->getName() . ')</span>',
33 34
      '#description' => $description,
      '#default_value' => $value,
35
      '#attributes' => array('lang' => $language->getId()),
36 37 38 39 40 41 42 43 44 45 46 47 48 49
      '#field_suffix' => ' <div class="edit-date-format-suffix"><small id="edit-date-format-suffix">' . $format . '</small></div>',
      '#ajax' => array(
        'callback' => 'Drupal\config_translation\FormElement\DateFormat::ajaxSample',
        'event' => 'keyup',
        'progress' => array('type' => 'throbber', 'message' => NULL),
      ),
    );
  }

  /**
   * Ajax callback to render a sample of the input date format.
   *
   * @param array $form
   *   Form API array structure.
50
   * @param \Drupal\Core\Form\FormStateInterface $form_state
51 52 53 54 55 56 57
   *   Form state information.
   *
   * @return AjaxResponse
   *   Ajax response with the rendered sample date using the given format. If
   *   the given format cannot be identified or was empty, the response will
   *   be empty as well.
   */
58
  public static function ajaxSample(array $form, FormStateInterface $form_state) {
59 60
    $response = new AjaxResponse();

61
    $format_value = NestedArray::getValue($form_state->getValues(), $form_state->getTriggeringElement()['#array_parents']);
62 63 64 65
    if (!empty($format_value)) {
      // Format the date with a custom date format with the given pattern.
      // The object is not instantiated in an Ajax context, so $this->t()
      // cannot be used here.
66
      $format = t('Displayed as %date_format', array('%date_format' => \Drupal::service('date.formatter')->format(REQUEST_TIME, 'custom', $format_value)));
67 68 69 70 71 72 73 74 75 76 77

      // Return a command instead of a string, since the Ajax framework
      // automatically prepends an additional empty DIV element for a string,
      // which breaks the layout.
      $response->addCommand(new ReplaceCommand('#edit-date-format-suffix', '<small id="edit-date-format-suffix">' . $format . '</small>'));
    }

    return $response;
  }

}