You are here

class SmartdateFieldBuilder in Smart Date 3.4.x

Same name and namespace in other branches
  1. 3.3.x src/Plugin/diff/Field/SmartdateFieldBuilder.php \Drupal\smart_date\Plugin\diff\Field\SmartdateFieldBuilder

Plugin to diff text fields.

Plugin annotation


@FieldDiffBuilder(
  id = "smartdate_field_diff_builder",
  label = @Translation("Smart Date Field Diff"),
  field_types = {
    "smartdate"
  },
)

Hierarchy

Expanded class hierarchy of SmartdateFieldBuilder

File

src/Plugin/diff/Field/SmartdateFieldBuilder.php, line 22

Namespace

Drupal\smart_date\Plugin\diff\Field
View source
class SmartdateFieldBuilder extends FieldDiffBuilderBase {
  use SmartDateTrait;

  /**
   * {@inheritdoc}
   */
  public function build(FieldItemListInterface $field_items) {
    $result = [];
    $format = \Drupal::entityTypeManager()
      ->getStorage('smart_date_format')
      ->load($this->configuration['format']);

    // Every item from $field_items is of type FieldItemInterface.
    foreach ($field_items as $field_key => $field_item) {
      $values = $field_item
        ->getValue();
      if (!isset($values['value'])) {
        continue;
      }
      if (!isset($values['end_value'])) {
        if (isset($values['duration']) && $values['duration'] > 0) {
          $values['end_value'] = $values['value'] + $values['duration'] * 60;
        }
        else {
          $values['end_value'] = $values['value'];
        }
      }
      $date_value = static::formatSmartDate($values['value'], $values['end_value'], $format
        ->getOptions(), NULL, 'string');
      $result[$field_key][] = $this
        ->t('Date: @date_value', [
        '@date_value' => $date_value,
      ]);
    }
    return $result;
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    unset($form['format_type']);

    // Change the description of the timezone_override element.
    if (isset($form['timezone_override'])) {
      $form['timezone_override']['#description'] = $this
        ->t('The time zone selected here will be used unless overridden on an individual date.');
    }

    // Ask the user to choose a Smart Date Format.
    $formatOptions = [];
    $smartDateFormats = \Drupal::entityTypeManager()
      ->getStorage('smart_date_format')
      ->loadMultiple();
    foreach ($smartDateFormats as $type => $format) {
      if ($format instanceof SmartDateFormat) {
        $formatted = static::formatSmartDate(time(), time() + 3600, $format
          ->getOptions(), NULL, 'string');
        $formatOptions[$type] = $format
          ->label() . ' (' . $formatted . ')';
      }
    }
    $form['format'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Smart Date Format'),
      '#description' => $this
        ->t('Choose which display configuration to use.'),
      '#default_value' => 'default',
      '#options' => $formatOptions,
    ];
    return parent::buildConfigurationForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $this->configuration['timezone_override'] = $form_state
      ->getValue('timezone_override');
    $this->configuration['format'] = $form_state
      ->getValue('format');
    parent::submitConfigurationForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    $default_configuration = array(
      'timezone_override' => 0,
      'format' => 'default',
    );
    $default_configuration += parent::defaultConfiguration();
    return $default_configuration;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SmartdateFieldBuilder::build public function
SmartdateFieldBuilder::buildConfigurationForm public function
SmartdateFieldBuilder::defaultConfiguration public function
SmartdateFieldBuilder::submitConfigurationForm public function
SmartDateTrait::addRangeClasses protected function Add spans provides classes to allow the dates and times to be styled.
SmartDateTrait::addTimeWrapper protected function Add spans provides classes to allow the dates and times to be styled.
SmartDateTrait::arrayToRender protected static function Helper function to turn a simple, nested array into a render array.
SmartDateTrait::formatSmartDate public static function Creates a formatted date value as a string.
SmartDateTrait::isAllDay public static function Evaluates whether or not a provided range is "all day".
SmartDateTrait::loadSmartDateFormat public static function Load a Smart Date Format from a format name.
SmartDateTrait::rangeDateReduce protected static function Reduce duplication in a provided date range.
SmartDateTrait::rangeFormat protected static function Format a provided range, using provided settings.
SmartDateTrait::settingsFormatNoDate public static function Removes date tokens from format settings.
SmartDateTrait::settingsFormatNoTime public static function Removes time tokens from format settings.
SmartDateTrait::settingsFormatNoTz public static function Removes timezone tokens from time settings.
SmartDateTrait::supportsDateAugmenter public function Explicitly declare support for the Date Augmenter API. 1
SmartDateTrait::timeFormat protected static function Helper function to apply time formats.
SmartDateTrait::viewElements public function 4