DateRecurModularWidgetBase.php in Recurring Date Field Modular Widgets 2.x
File
src/Plugin/Field/FieldWidget/DateRecurModularWidgetBase.php
View source
<?php
declare (strict_types=1);
namespace Drupal\date_recur_modular\Plugin\Field\FieldWidget;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\date_recur_modular\DateRecurModularUtilityTrait;
use Drupal\date_recur\Plugin\Field\FieldType\DateRecurItem;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class DateRecurModularWidgetBase extends WidgetBase implements ContainerFactoryPluginInterface {
use DateRecurModularUtilityTrait;
protected $configFactory;
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, ConfigFactoryInterface $configFactory) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings);
$this->configFactory = $configFactory;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['third_party_settings'], $container
->get('config.factory'));
}
protected function getModes() : array {
return [];
}
protected function getMode(DateRecurItem $item) : ?string {
return NULL;
}
public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
foreach ($values as $delta => &$value) {
$start = $value['start'] ?? NULL;
$end = $value['end'] ?? NULL;
$timeZone = $value['time_zone'] ?? NULL;
$mode = $value['mode'] ?? NULL;
if (!$start instanceof DrupalDateTime || !$end instanceof DrupalDateTime || !is_string($timeZone) || !is_string($mode)) {
$value = [];
}
}
return $values;
}
}