You are here

protected function DateRecurModularAlphaWidget::getMode in Recurring Date Field Modular Widgets 2.x

Same name and namespace in other branches
  1. 8 src/Plugin/Field/FieldWidget/DateRecurModularAlphaWidget.php \Drupal\date_recur_modular\Plugin\Field\FieldWidget\DateRecurModularAlphaWidget::getMode()
  2. 3.x src/Plugin/Field/FieldWidget/DateRecurModularAlphaWidget.php \Drupal\date_recur_modular\Plugin\Field\FieldWidget\DateRecurModularAlphaWidget::getMode()

Determine the best suitable mode for a date recur field item.

Parameters

\Drupal\date_recur\Plugin\Field\FieldType\DateRecurItem $item: A date recur field item.

Return value

string|null A mode.

Overrides DateRecurModularWidgetBase::getMode

File

src/Plugin/Field/FieldWidget/DateRecurModularAlphaWidget.php, line 86

Class

DateRecurModularAlphaWidget
Date recur alpha widget.

Namespace

Drupal\date_recur_modular\Plugin\Field\FieldWidget

Code

protected function getMode(DateRecurItem $item) : ?string {
  try {
    $helper = $item
      ->getHelper();
  } catch (DateRecurHelperArgumentException $e) {
    return NULL;
  }
  $rules = $helper
    ->getRules();
  $rule = reset($rules);
  if (FALSE === $rule) {

    // This widget supports one RRULE per field value.
    return NULL;
  }
  $frequency = $rule
    ->getFrequency();
  $parts = $rule
    ->getParts();
  if ('DAILY' === $frequency) {

    /** @var int|null $count */
    $count = $parts['COUNT'] ?? NULL;
    return $count && $count > 1 ? static::MODE_MULTIDAY : static::MODE_ONCE;
  }
  elseif ('WEEKLY' === $frequency) {

    /** @var int|null $interval */
    $interval = $parts['INTERVAL'] ?? NULL;
    return [
      1 => static::MODE_WEEKLY,
      2 => static::MODE_FORTNIGHTLY,
    ][$interval] ?? NULL;
  }
  elseif ('MONTHLY' === $frequency) {
    return static::MODE_MONTHLY;
  }
  return NULL;
}