You are here

public function MomentFormat::moment in Twig Extender 4.x

Same name and namespace in other branches
  1. 8.4 modules/twig_extender_extras/src/Plugin/TwigPlugin/MomentFormat.php \Drupal\twig_extender_extras\Plugin\TwigPlugin\MomentFormat::moment()
  2. 8.2 modules/twig_extender_extras/src/Plugin/TwigPlugin/MomentFormat.php \Drupal\twig_extender_extras\Plugin\TwigPlugin\MomentFormat::moment()
  3. 8.3 modules/twig_extender_extras/src/Plugin/TwigPlugin/MomentFormat.php \Drupal\twig_extender_extras\Plugin\TwigPlugin\MomentFormat::moment()

Format a date with moment php.

Parameters

mixed $date: The element array whose children are to be identified. Passed by reference.

string $format: Boolean to indicate whether the children should be sorted by weight.

mixed $timezone: Boolean to indicate whether the children should be sorted by weight.

mixed $js: Boolean to indicate whether the children should be sorted by weight.

Return value

array Array for render.

Throws

\Exception

File

modules/twig_extender_extras/src/Plugin/TwigPlugin/MomentFormat.php, line 38

Class

MomentFormat
Provide helper methods for Drupal render elements.

Namespace

Drupal\twig_extender_extras\Plugin\TwigPlugin

Code

public function moment($date, $format, $timezone = NULL, $js = FALSE) {
  $moment = $this
    ->getMoment($date, $timezone);
  $value = $moment
    ->format($format);
  if ($js === TRUE) {
    $value = $moment
      ->format($format, new MomentJs());
  }
  $build = [
    '#cache' => [
      'contexts' => [
        'languages',
        'timezone',
      ],
    ],
    '#markup' => $value,
  ];
  return $build;
}