Commit ba90c3d7 authored by Christopher C. Wells's avatar Christopher C. Wells Committed by Christopher C. Wells
Browse files

Issue #3249457 by wells, liquidcms, bakulahluwalia: Views support

parent 73db421a
Loading
Loading
Loading
Loading
+17 −5
Changes for src/Twig/CalendarLinkTwigExtension.php: 17 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@
namespace Drupal\calendar_link\Twig;

use Drupal\calendar_link\CalendarLinkException;
use Drupal\Component\Utility\Html;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
@@ -230,16 +231,27 @@ class CalendarLinkTwigExtension extends AbstractExtension {
      $date = $date->date;
    }

    // Drupal date time. E.g. `node.field_start.date`.
    if ($date instanceof DrupalDateTime) {
      $date = $date->getPhpDateTime();
    if ($date instanceof \DateTime) {
      return $date;
    }

    if ($date instanceof \DateTime) {
    if ($date instanceof DrupalDateTime) {
      return $date;
    }

    throw new CalendarLinkException('Could not get valid \DateTime object from input.');
    // Attempt to parse an HTML `time` element (Views default behavior). Only
    // the default formatter is supported as other formatters do not guarantee
    // accurate timezone data.
    /** @var \DOMElement $element */
    $element = Html::load($date)->getElementsByTagName('time')->item(0);
    if ($element && $element->hasAttribute('datetime')) {
      $date = new DrupalDateTime($element->getAttribute('datetime'));
      if (!$date->hasErrors()) {
        return $date->getPhpDateTime();
      }
    }

    throw new CalendarLinkException("Could not get valid \DateTime object from input: $date");
  }

}
+29 −6
Changes for tests/src/Kernel/CalendarLinkTwigExtensionTest.php: 29 added lines, 6 removed lines.
Original line number Diff line number Diff line
@@ -53,16 +53,14 @@ class CalendarLinkTwigExtensionTest extends KernelTestBase {

  /**
   * Tests the "calendar_link" Twig function.
   *
   * @dataProvider calendarLinkDataProvider
   */
  public function testCalendarLinkFunction() {
    $template = "{% set title = 'title'|t %}{% set startDate = date('2019-02-24 10:00', 'Etc/UTC') %}{% set endDate = date('2019-02-24 12:00', 'Etc/UTC') %}{% set link = calendar_link('ics', title, startDate, endDate, false, 'description', 'location') %}<a href=\"{{ link }}\">Add to calendar</a>";
    $expected_template_output = '<a href="data:text/calendar;charset=utf8;base64,QkVHSU46VkNBTEVOREFSDQpWRVJTSU9OOjIuMA0KQkVHSU46VkVWRU5UDQpVSUQ6YTc4ZDRjM2NjNzA3YzRjZGU3NjBiYWQzZmJmZjhlYTENClNVTU1BUlk6dGl0bGUNCkRUU1RBUlQ7VFpJRD1FdGMvVVRDOjIwMTkwMjIzVDIzMDAwMA0KRFRFTkQ7VFpJRD1FdGMvVVRDOjIwMTkwMjI0VDAxMDAwMA0KREVTQ1JJUFRJT046ZGVzY3JpcHRpb24NCkxPQ0FUSU9OOmxvY2F0aW9uDQpFTkQ6VkVWRU5UDQpFTkQ6VkNBTEVOREFS">Add to calendar</a>';

  public function testCalendarLinkFunction($template, $expected_output) {
    /** @var \Drupal\Core\Template\TwigEnvironment $environment */
    $environment = \Drupal::service('twig');

    $output = (string) $environment->renderInline($template);
    $this->assertEquals($expected_template_output, $output);
    $this->assertEquals($expected_output, $output);
  }

  /**
@@ -82,4 +80,29 @@ class CalendarLinkTwigExtensionTest extends KernelTestBase {
    );
  }

  /**
   * Provides example data for `calendar_link` tests.
   *
   * @return \string[][]
   *   Test values and expected output.
   *
   * @see CalendarLinkTwigExtensionTest::testCalendarLinkFunction()
   *
   * @todo Add render array and field item object tests.
   */
  public function calendarLinkDataProvider(): array {
    return [
      // Dates as simple objects.
      [
        "{% set title = 'title'|t %}{% set startDate = date('2019-02-24 10:00', 'Etc/UTC') %}{% set endDate = date('2019-02-24 12:00', 'Etc/UTC') %}{% set link = calendar_link('ics', title, startDate, endDate, false, 'description', 'location') %}<a href=\"{{ link }}\">Add to calendar</a>",
        '<a href="data:text/calendar;charset=utf8;base64,QkVHSU46VkNBTEVOREFSDQpWRVJTSU9OOjIuMA0KQkVHSU46VkVWRU5UDQpVSUQ6YTc4ZDRjM2NjNzA3YzRjZGU3NjBiYWQzZmJmZjhlYTENClNVTU1BUlk6dGl0bGUNCkRUU1RBUlQ7VFpJRD1FdGMvVVRDOjIwMTkwMjIzVDIzMDAwMA0KRFRFTkQ7VFpJRD1FdGMvVVRDOjIwMTkwMjI0VDAxMDAwMA0KREVTQ1JJUFRJT046ZGVzY3JpcHRpb24NCkxPQ0FUSU9OOmxvY2F0aW9uDQpFTkQ6VkVWRU5UDQpFTkQ6VkNBTEVOREFS">Add to calendar</a>',
      ],
      // Dates as `<time>` HTML elements (Views behavior).
      [
        "{% set link = calendar_link('yahoo', 'title', '<time datetime=\"2022-03-21T16:00:00Z\" class=\"datetime\">Mon, 03/21/2022 - 09:00</time>', '<time datetime=\"2022-03-26T00:00:00Z\" class=\"datetime\">Fri, 03/25/2022 - 17:00</time>', false, 'description', 'location') %}<a href=\"{{ link }}\">Add to calendar</a>",
        '<a href="https://calendar.yahoo.com/?v=60&amp;view=d&amp;type=20&amp;ST=20220321T160000Z&amp;ET=20220326T000000Z&amp;TITLE=title&amp;DESC=description&amp;in_loc=location">Add to calendar</a>',
      ],
    ];
  }

}