You are here

abstract class AddToCalTypeBase in Add to Cal 8

Hierarchy

Expanded class hierarchy of AddToCalTypeBase

3 files declare their use of AddToCalTypeBase
Generic.php in src/Plugin/AddToCal/Type/Generic.php
Google.php in src/Plugin/AddToCal/Type/Google.php
Yahoo.php in src/Plugin/AddToCal/Type/Yahoo.php

File

src/AddToCalTypeBase.php, line 15

Namespace

Drupal\addtocal
View source
abstract class AddToCalTypeBase extends PluginBase implements AddToCalTypeInterface {
  const RF3339_FORMAT = 'Ymd\\THis\\Z';
  const DT_FORMAT = 'Ymd\\THis';

  /**
   * The date formatter service.
   *
   * @var \Drupal\Core\Datetime\DateFormatterInterface
   */
  protected $dateFormatter;

  /**
   * Constructs a AddToCalPluginBase object.
   *
   * @param array $configuration
   * @param string $plugin_id
   * @param mixed $plugin_definition
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, DateFormatterInterface $date_formatter) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->dateFormatter = $date_formatter;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('date.formatter'));
  }

  /**
   * {@inheritdoc}
   */
  public function getName() {
    return $this->pluginDefinition['name'];
  }

  /**
   * @param \Drupal\Core\Entity\EntityInterface $entity
   * @param array $settings
   * @param int $delta
   * @return array
   */
  protected function extractEventDetails(EntityInterface $entity, array $settings, $delta) {
    $entity_type = $entity
      ->getEntityTypeId();
    $field_name = $settings['field_name'];

    /** @var DateTimeFieldItemList $dates */
    $dates = $entity
      ->get($field_name);

    // Date range field has different structure
    if (!empty($dates[$delta]->start_date) && !empty($dates[$delta]->end_date)) {
      $start_date_object = $dates[$delta]->start_date;
      $end_date_object = $dates[$delta]->end_date;
    }
    elseif (!empty($dates[$delta]->start_date) && empty($dates[$delta]->end_date)) {
      $start_date_object = $end_date_object = $dates[$delta]->start_date;
    }
    else {
      $start_date_object = $end_date_object = $dates[$delta]->date;
    }
    $timezone = !empty($settings['timezone_override']) ? $settings['timezone_override'] : NULL;
    $start_date = $this->dateFormatter
      ->format($start_date_object
      ->getTimestamp(), 'custom', $settings['date_format'], $timezone);
    $end_date = $this->dateFormatter
      ->format($end_date_object
      ->getTimestamp(), 'custom', $settings['date_format'], $timezone);
    if ($settings['location']['value']) {
      $location = $this
        ->extractFieldValue($entity, $settings['location']['value']);
    }
    else {

      // @TODO: Token replace
      $location = $settings['location']['tokenized'];
    }
    if ($settings['description']['value']) {
      $description = $this
        ->extractFieldValue($entity, $settings['description']['value']);
    }
    else {

      // @TODO: Token replace
      $description = $settings['description']['tokenized'];
    }
    if (strlen($description) > 1024) {
      $description = Unicode::truncate($description, 1024, TRUE, TRUE);
    }
    $url = $entity
      ->toUrl()
      ->setAbsolute()
      ->toString();
    $title = $entity
      ->label();
    $overridable_items = [
      'override_status' => false,
      'title' => $title,
      'location' => $location,
      'description' => $description,
    ];

    // Trigger hook_addtocal_alter().
    // Allow modules to override addtocal fields.
    \Drupal::service('module_handler')
      ->alter('addtocal', $overridable_items);

    // If any override was found - set the fields to the override values.
    if ($overridable_items['override_status']) {
      $title = $overridable_items['title'];
      $location = $overridable_items['location'];
      $description = $overridable_items['description'];
    }
    return [
      'title' => $title,
      'start' => $start_date,
      'end' => $end_date,
      'timezone' => $timezone,
      'location' => $location,
      'description' => $description,
      'entity_id' => $entity
        ->id(),
      'entity_type' => $entity_type,
      'url' => $url,
      'rfc3339' => $this
        ->rfc3339Date($start_date_object, $end_date_object, $timezone),
    ];
  }

  /**
   * @param \Drupal\Core\Entity\EntityInterface $entity
   * @param $field_name
   * @return string
   */
  protected function extractFieldValue(EntityInterface $entity, $field_name) {
    $output = '';
    if (!empty($field_name)) {
      $value = $entity
        ->get($field_name)
        ->getValue();
      $instance = FieldStorageConfig::loadByName($entity
        ->getEntityTypeId(), $field_name);
      if ($instance
        ->getType() == 'address') {
        $address = $value[0];
        $string = '';
        if (!empty($address['address_line1'])) {
          $string .= $address['address_line1'] . ' ';
        }
        if (!empty($address['address_line2'])) {
          $string .= $address['address_line2'] . ', ';
        }
        if (!empty($address['locality'])) {
          $string .= $address['locality'] . ', ';
        }
        if (!empty($address['administrative_area'])) {
          $string .= $address['administrative_area'] . ' ';
        }
        if (!empty($address['postal_code'])) {
          $string .= $address['postal_code'] . ', ';
        }
        if (!empty($address['country_code'])) {
          $string .= $address['country_code'];
        }
        $output = $string;
      }
      else {
        $replace_strings = [
          ' ' => '',
          '<br />' => '',
          PHP_EOL => '',
        ];
        $output = $value[0]['value'];
        foreach ($replace_strings as $search => $replace) {
          $output = str_replace($search, $replace, $output);
        }
      }
    }
    return strip_tags($output);
  }

  /**
   * Returns an array containing RFC 3339 formatted start and end dates.
   *
   * @param \Drupal\Core\Datetime\DrupalDateTime $start
   * @param \Drupal\Core\Datetime\DrupalDateTime $end
   * @param $timezone
   * @return array
   */
  protected function rfc3339Date(DrupalDateTime $start, DrupalDateTime $end, $timezone) {
    $start_timestamp = $start
      ->getTimestamp();
    $end_timestamp = $end
      ->getTimestamp();
    $start_date = gmdate(self::RF3339_FORMAT, $start_timestamp);
    $end_date = gmdate(self::RF3339_FORMAT, $end_timestamp);
    return [
      'start' => $start_date,
      'end' => $end_date,
      'both' => $start_date . '/' . $end_date,
      'local_start' => $this->dateFormatter
        ->format($start_timestamp, 'custom', self::DT_FORMAT, $timezone),
      'local_end' => $this->dateFormatter
        ->format($end_timestamp, 'custom', self::DT_FORMAT, $timezone),
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AddToCalTypeBase::$dateFormatter protected property The date formatter service.
AddToCalTypeBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 1
AddToCalTypeBase::DT_FORMAT constant
AddToCalTypeBase::extractEventDetails protected function
AddToCalTypeBase::extractFieldValue protected function
AddToCalTypeBase::getName public function Return the name of the type plugin. Overrides AddToCalTypeInterface::getName
AddToCalTypeBase::RF3339_FORMAT constant
AddToCalTypeBase::rfc3339Date protected function Returns an array containing RFC 3339 formatted start and end dates.
AddToCalTypeBase::__construct public function Constructs a AddToCalPluginBase object. Overrides PluginBase::__construct 1
AddToCalTypeInterface::downloadSubmit public function 3
AddToCalTypeInterface::generateStructure public function 3
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.