You are here

class CalendarLinkTwigExtension in Calendar Link 2.x

Same name and namespace in other branches
  1. 8 src/Twig/CalendarLinkTwigExtension.php \Drupal\calendar_link\Twig\CalendarLinkTwigExtension

Twig extensions class for the `calendar_link` and `calender_links` functions.

@package Drupal\calendar_link\Twig

Hierarchy

Expanded class hierarchy of CalendarLinkTwigExtension

1 file declares its use of CalendarLinkTwigExtension
CalendarLinkTwigExtensionTest.php in tests/src/Kernel/CalendarLinkTwigExtensionTest.php
1 string reference to 'CalendarLinkTwigExtension'
calendar_link.services.yml in ./calendar_link.services.yml
calendar_link.services.yml
1 service uses CalendarLinkTwigExtension
calendar_link.twig_extension in ./calendar_link.services.yml
Drupal\calendar_link\Twig\CalendarLinkTwigExtension

File

src/Twig/CalendarLinkTwigExtension.php, line 16

Namespace

Drupal\calendar_link\Twig
View source
class CalendarLinkTwigExtension extends \Twig_Extension {
  use StringTranslationTrait;

  /**
   * Available link types (generators).
   *
   * @var array
   *
   * @see \Spatie\CalendarLinks\Link
   */
  protected static $types = [
    'google' => 'Google',
    'ics' => 'iCal',
    'yahoo' => 'Yahoo!',
    'webOutlook' => 'Outlook.com',
  ];

  /**
   * {@inheritdoc}
   */
  public function getFunctions() {
    return [
      new \Twig_SimpleFunction('calendar_link', [
        $this,
        'calendarLink',
      ]),
      new \Twig_SimpleFunction('calendar_links', [
        $this,
        'calendarLinks',
      ]),
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getName() {
    return 'calendar_link';
  }

  /**
   * Create a calendar link.
   *
   * @param string $type
   *   Generator key to use for link building.
   * @param string $title
   *   Calendar entry title.
   * @param \Drupal\Core\Datetime\DrupalDateTime|\DateTime $from
   *   Calendar entry start date and time.
   * @param \Drupal\Core\Datetime\DrupalDateTime|\DateTime $to
   *   Calendar entry end date and time.
   * @param bool $all_day
   *   Indicator for an "all day" calendar entry.
   * @param string $description
   *   Calendar entry description.
   * @param string $address
   *   Calendar entry address.
   *
   * @return string
   *   URL for the specific calendar type.
   */
  public function calendarLink($type, $title, $from, $to, $all_day = FALSE, $description = '', $address = '') {
    if (!isset(self::$types[$type])) {
      throw new CalendarLinkException('Invalid calendar link type.');
    }
    try {
      if ($from instanceof DrupalDateTime) {
        $from = $from
          ->getPhpDateTime();
      }
      if ($to instanceof DrupalDateTime) {
        $to = $to
          ->getPhpDateTime();
      }
      $link = Link::create($title, $from, $to, $all_day);
    } catch (InvalidLink $e) {
      throw new CalendarLinkException('Invalid calendar link data.');
    }
    if ($description) {
      $link
        ->description($description);
    }
    if ($address) {
      $link
        ->address($address);
    }
    return $link
      ->{$type}();
  }

  /**
   * Create links for all calendar types.
   *
   * @param string $title
   *   Calendar entry title.
   * @param \Drupal\Core\Datetime\DrupalDateTime|\DateTime $from
   *   Calendar entry start date and time.
   * @param \Drupal\Core\Datetime\DrupalDateTime|\DateTime $to
   *   Calendar entry end date and time.
   * @param bool $all_day
   *   Indicator for an "all day" calendar entry.
   * @param string $description
   *   Calendar entry description.
   * @param string $address
   *   Calendar entry address.
   *
   * @return array
   *   - type_key: Machine key for the calendar type.
   *   - type_name: Human-readable name for the calendar type.
   *   - url: URL for the specific calendar type.
   */
  public function calendarLinks($title, $from, $to, $all_day = FALSE, $description = '', $address = '') {
    $links = [];
    foreach (self::$types as $type => $name) {
      $links[$type] = [
        'type_key' => $type,
        'type_name' => $name,
        'url' => $this
          ->calendarLink($type, $title, $from, $to, $all_day, $description, $address),
      ];
    }
    return $links;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CalendarLinkTwigExtension::$types protected static property Available link types (generators).
CalendarLinkTwigExtension::calendarLink public function Create a calendar link.
CalendarLinkTwigExtension::calendarLinks public function Create links for all calendar types.
CalendarLinkTwigExtension::getFunctions public function
CalendarLinkTwigExtension::getName public function
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.