class CalendarLinkTwigExtension in Calendar Link 8
Same name and namespace in other branches
- 2.x src/Twig/CalendarLinkTwigExtension.php \Drupal\calendar_link\Twig\CalendarLinkTwigExtension
Class CalendarLinkTwigExtension.
@package Drupal\calendar_link\Twig
Hierarchy
- class \Drupal\calendar_link\Twig\CalendarLinkTwigExtension extends \Drupal\calendar_link\Twig\Twig_Extension uses StringTranslationTrait
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'
1 service uses CalendarLinkTwigExtension
File
- src/
Twig/ CalendarLinkTwigExtension.php, line 16
Namespace
Drupal\calendar_link\TwigView 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($this
->t('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($this
->t('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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CalendarLinkTwigExtension:: |
protected static | property | Available link types (generators). | |
CalendarLinkTwigExtension:: |
public | function | Create a calendar link. | |
CalendarLinkTwigExtension:: |
public | function | Create links for all calendar types. | |
CalendarLinkTwigExtension:: |
public | function | ||
CalendarLinkTwigExtension:: |
public | function | ||
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |