You are here

class TimeFieldStandardWidget in Timefield 1.0.x

Plugin implementation of the 'timefield_standard_widget' widget.

Plugin annotation


@FieldWidget(
 id = "timefield_standard_widget",
 label = @Translation("Timefield"),
 field_types = {"timefield"}
)

Hierarchy

Expanded class hierarchy of TimeFieldStandardWidget

File

src/Plugin/Field/FieldWidget/TimeFieldStandardWidget.php, line 30

Namespace

Drupal\timefield\Plugin\Field\FieldWidget
View source
class TimeFieldStandardWidget extends WidgetBase implements ContainerFactoryPluginInterface {

  /**
   * Entity type manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Field widget display plugin manager.
   *
   * @var \Drupal\entity_browser\FieldWidgetDisplayManager
   */
  protected $fieldDisplayManager;

  /**
   * The depth of the delete button.
   *
   * This property exists so it can be changed if subclasses.
   *
   * @var int
   */
  protected static $deleteDepth = 4;

  /**
   * The module handler interface.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  /**
   * If triggering element was hidden target_id element.
   *
   * @var bool
   */
  protected $entityBrowserValueUpdated;

  /**
   * If triggering element was hidden target_id element.
   *
   * @var \Drupal\Core\Asset\LibraryDiscovery
   */
  protected $libraryDiscovery;

  /**
   * Constructs widget plugin.
   *
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
   *   The definition of the field to which the widget is associated.
   * @param array $settings
   *   The widget settings.
   * @param array $third_party_settings
   *   Any third party settings.
   * @param \Drupal\Core\Asset\LibraryDiscovery $libraryDiscovery
   *   The library discovery.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface|null $module_handler
   *   The module handler.
   */
  public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, LibraryDiscovery $libraryDiscovery, ModuleHandlerInterface $module_handler) {
    parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings);
    $this->libraryDiscovery = $libraryDiscovery;
    $this->moduleHandler = $module_handler;
  }

  /**
   * {@inheritdoc}
   */
  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('library.discovery'), $container
      ->get('module_handler'));
  }

  /**
   * {@inheritdoc}
   */
  public static function defaultSettings() {
    $library = \Drupal::service('library.discovery')
      ->getLibraryByName('timefield', 'timepicker');
    return [
      'disable_plugin' => empty($library) ? TRUE : FALSE,
      'input_format' => [
        'separator' => ':',
        'showLeadingZero' => FALSE,
        'showMinutesLeadingZero' => TRUE,
        'showPeriod' => TRUE,
        'periodSeparator' => '',
        'showHours' => TRUE,
        'showMinutes' => TRUE,
        'am_text' => 'AM',
        'pm_text' => 'PM',
        'minute_interval' => 5,
        'showCloseButton' => FALSE,
        'closeButtonText' => 'Done',
        'showNowButton' => FALSE,
        'nowButtonText' => 'Now',
        'showDeselectButton' => FALSE,
        'deselectButtonText' => 'Deselect',
        'myPosition' => 'left top',
        'atPosition' => 'left bottom',
      ],
    ] + parent::defaultSettings();
  }

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state) {
    $elements = parent::settingsForm($form, $form_state);
    $library = $this->libraryDiscovery
      ->getLibraryByName('timefield', 'timepicker');
    if (empty($library)) {
      $this
        ->messenger()
        ->addWarning($this
        ->t("You will not have enhanced time input widget without downloading the plugin. %link", [
        '%link' => Link::fromTextAndUrl("Read installation instructions here.", Url::fromUserInput('http://drupalcode.org/project/timefield.git/blob_plain/HEAD:/README.txt')),
      ]));
    }
    $elements['disable_plugin'] = [
      '#title' => $this
        ->t('Disable jQuery Timepicker plugin.'),
      '#type' => 'checkbox',
      '#default_value' => $this
        ->getSetting('disable_plugin'),
      '#description' => $this
        ->t('Do not use jQuery Timepicker plugin for input.'),
      '#disabled' => empty($library),
      '#attributes' => [
        'name' => 'disable_plugin',
      ],
    ];
    $elements['separator'] = [
      '#title' => $this
        ->t('Hour and Minute Separator'),
      '#type' => 'textfield',
      '#default_value' => $this
        ->getSetting('separator'),
      '#size' => 10,
      '#description' => $this
        ->t('The character to use to separate hours and minutes.'),
    ];
    $elements['showLeadingZero'] = [
      '#title' => $this
        ->t('Show Leading Zero for Hour'),
      '#type' => 'checkbox',
      '#default_value' => $this
        ->getSetting('showLeadingZero'),
      '#description' => $this
        ->t('Whether or not to show a leading zero for hours < 10.'),
    ];
    $elements['showPeriod'] = [
      '#title' => $this
        ->t('Show AM/PM Label'),
      '#type' => 'checkbox',
      '#default_value' => $this
        ->getSetting('showPeriod'),
      '#description' => $this
        ->t('Whether or not to show AM/PM on the input textfield both on the widget and in the text field after selecting the time with the widget.'),
    ];
    $elements['periodSeparator'] = [
      '#title' => $this
        ->t('What character should appear between the time and the Period (AM/PM)'),
      '#type' => 'textfield',
      '#default_value' => $this
        ->getSetting('periodSeparator'),
      '#size' => 10,
      '#description' => $this
        ->t('The character to use to separate the time from the time period (AM/PM).'),
    ];
    $elements['am_text'] = [
      '#title' => $this
        ->t('AM text'),
      '#type' => 'textfield',
      '#default_value' => $this
        ->getSetting('am_text'),
      '#size' => 10,
    ];
    $elements['pm_text'] = [
      '#title' => $this
        ->t('PM text'),
      '#type' => 'textfield',
      '#default_value' => $this
        ->getSetting('pm_text'),
      '#size' => 10,
    ];
    $elements['showCloseButton'] = [
      '#title' => $this
        ->t('Show a Button to Close the Picker Widget'),
      '#type' => 'checkbox',
      '#default_value' => $this
        ->getSetting('showCloseButton'),
      '#states' => [
        'invisible' => [
          ':input[name=disable_plugin]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $elements['closeButtonText'] = [
      '#title' => $this
        ->t('Close Button text'),
      '#type' => 'textfield',
      '#default_value' => $this
        ->getSetting('closeButtonText'),
      '#size' => 10,
      '#states' => [
        'invisible' => [
          ':input[name=disable_plugin]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $elements['showNowButton'] = [
      '#title' => $this
        ->t('Show a Button to Select the Current Time'),
      '#type' => 'checkbox',
      '#default_value' => $this
        ->getSetting('showNowButton'),
      '#states' => [
        'invisible' => [
          ':input[name=disable_plugin]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $elements['nowButtonText'] = [
      '#title' => $this
        ->t('Now Button text'),
      '#type' => 'textfield',
      '#default_value' => $this
        ->getSetting('nowButtonText'),
      '#size' => 10,
      '#states' => [
        'invisible' => [
          ':input[name=disable_plugin]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $elements['showDeselectButton'] = [
      '#title' => $this
        ->t('Show a Button to Deselect the time in the Picker Widget'),
      '#type' => 'checkbox',
      '#default_value' => $this
        ->getSetting('showDeselectButton'),
      '#states' => [
        'invisible' => [
          ':input[name=disable_plugin]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $elements['deselectButtonText'] = [
      '#title' => $this
        ->t('Deselect Button text'),
      '#type' => 'textfield',
      '#default_value' => $this
        ->getSetting('deselectButtonText'),
      '#size' => 10,
      '#states' => [
        'invisible' => [
          ':input[name=disable_plugin]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $elements['myPosition'] = [
      '#title' => $this
        ->t('my Position'),
      '#type' => 'select',
      '#default_value' => $this
        ->getSetting('myPosition'),
      '#options' => array_combine([
        'left top',
        'left center',
        'left bottom',
        'center top',
        'center center',
        'center bottom',
        'right top',
        'right center',
        'right bottom',
      ], [
        'left top',
        'left center',
        'left bottom',
        'center top',
        'center center',
        'center bottom',
        'right top',
        'right center',
        'right bottom',
      ]),
      '#description' => $this
        ->t('Corner of the timpicker widget dialog to position. See !jquery_info for more info.', [
        '!jquery_info' => Link::fromTextAndUrl($this
          ->t("jQuery UI Position documentation"), Url::fromUri('http://jqueryui.com/demos/position')),
      ]),
      '#states' => [
        'invisible' => [
          ':input[name=disable_plugin]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $elements['atPosition'] = [
      '#title' => $this
        ->t('at Position'),
      '#type' => 'select',
      '#options' => array_combine([
        'left top',
        'left center',
        'left bottom',
        'center top',
        'center center',
        'center bottom',
        'right top',
        'right center',
        'right bottom',
      ], [
        'left top',
        'left center',
        'left bottom',
        'center top',
        'center center',
        'center bottom',
        'right top',
        'right center',
        'right bottom',
      ]),
      '#default_value' => $this
        ->getSetting('atPosition'),
      '#description' => $this
        ->t('Where to position "my Position" relative to input widget textfield See !jquery_info for more info.', [
        '!jquery_info' => Link::fromTextAndUrl($this
          ->t("jQuery UI Position documentation"), Url::fromUri('http://jqueryui.com/demos/position')),
      ]),
      '#states' => [
        'invisible' => [
          ':input[name=disable_plugin]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    return $elements;
  }

  /**
   * {@inheritdoc}
   */
  public function settingsSummary() {
    $summary = parent::settingsSummary();
    $summary[] = $this
      ->t('Number of summary rows: @rows', [
      '@rows' => $this
        ->getSetting('summary_rows'),
    ]);
    if ($this
      ->getSetting('show_summary')) {
      $summary[] = $this
        ->t('Summary field will always be visible');
    }
    return $summary;
  }

  /**
   * {@inheritdoc}
   */
  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
    $wrapper = FALSE;
    $instance_class = str_replace('_', '-', $items
      ->getName()) . "-" . $delta;
    $instance_settings = $this
      ->getSettings();
    $field_settings = $this
      ->getFieldSettings();
    if (!$instance_settings['disable_plugin']) {
      $js_settings = _timefield_js_settings($instance_class, $instance_settings);
      $context = [
        'type' => 'field',
        'items' => $items,
      ];
      $this->moduleHandler
        ->alter('timefield_js_settings', $js_settings, $context);
      $element['#attached']['library'][] = 'timefield/timepicker';
      $element['#attached']['library'][] = 'timefield/timefield';
      $element['#attached']['drupalSettings']['timefield'][$instance_class] = $js_settings;
    }
    $element += [
      '#delta' => $delta,
    ];
    if ($field_settings['weekly_summary_with_label']) {
      $wrapper = TRUE;
      $element['label'] = [
        '#title' => $this
          ->t('Label'),
        '#description' => $this
          ->t('Enter a label for the summary'),
        '#type' => 'textfield',
        '#default_value' => isset($items[$delta]->label) ? $items[$delta]->label : '',
        '#size' => 40,
        '#maxlength' => 60,
      ];
    }
    $value = isset($items[$delta]) ? timefield_integer_to_time($instance_settings, $items[$delta]->value) : '';
    $element['value'] = [
      '#type' => 'textfield',
      '#title' => Xss::filter($element['#title']),
      '#description' => Xss::filter($element['#description']),
      '#default_value' => $value,
      '#required' => $element['#required'],
      '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
      '#delta' => $delta,
      '#element_validate' => [
        [
          static::class,
          'validateTimeField',
        ],
      ],
      '#attributes' => [
        'class' => [
          'edit-timefield-timepicker',
          $instance_class,
        ],
      ],
    ];
    if ($field_settings['totime'] == 'required' || $field_settings['totime'] == 'optional') {
      $wrapper = TRUE;
      $value2 = isset($items[$delta]) ? timefield_integer_to_time($instance_settings, $items[$delta]->value2) : '';
      $element['value2'] = [
        '#type' => 'textfield',
        '#title' => $this
          ->t('End Time'),
        '#description' => $this
          ->t('Enter a time value, in any format'),
        '#default_value' => $value2,
        '#required' => $element['#required'],
        '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
        '#delta' => $delta,
        '#element_validate' => [
          [
            static::class,
            'validateTimeField',
          ],
        ],
        '#attributes' => [
          'class' => [
            'edit-timefield-timepicker',
            $instance_class,
          ],
        ],
      ];
    }
    if ($field_settings['weekly_summary'] || $field_settings['weekly_summary_with_label']) {
      $wrapper = TRUE;
      $days = isset($items[$delta]->mon) ? _timefield_weekly_summary_days_map($items[$delta]) : [];
      $weekDays = _timefield_weekly_summary_days();
      $element['days'] = [
        '#type' => 'container',
        '#description' => $this
          ->t('Select the days this schedule applies to'),
        '#attributes' => [
          'class' => [
            'edit-field-timefield-days',
          ],
        ],
      ];
      foreach ($weekDays as $key => $day) {
        $element[$key] = [
          '#title' => $day,
          '#type' => 'checkbox',
          '#default_value' => !empty($days) && $days[$key] != '0' ? 1 : 0,
          '#attributes' => [
            'class' => [
              'edit-field-timefield-days',
            ],
          ],
        ];
        if ($key == 'mon') {
          $element[$key]['#prefix'] = new FormattableMarkup('<b>@label</b>', [
            '@label' => 'Days',
          ]);
        }
        elseif ($key == 'sun') {
          $element[$key]['#suffix'] = $this
            ->t('Select the days this schedule applies to');
        }
      }
    }
    if ($wrapper) {
      $element['#theme_wrappers'][] = 'fieldset';
    }
    return $element;
  }

  /**
   * {@inheritdoc}
   *
   * Validation Callback; Timefield process field.
   */
  public static function validateTimeField($element, FormStateInterface $form_state) {

    // If empty, set to null.
    if (strlen($element['#value']) == 0) {
      if (!empty($element['#required'])) {
        $form_state
          ->setError($element, new TranslatableMarkup('@name field is required.', [
          '@name' => Html::escape($element['#title']),
        ]));
      }
      $form_state
        ->setValueForElement($element, NULL);
      return;
    }
    $date_value = date_parse($element['#value']);
    if ($date_value['error_count']) {
      $form_state
        ->setError($element, new TranslatableMarkup('Please enter the time in a valid format'));
    }
    else {
      $parsed_value = timefield_time_to_integer($element['#value']);
      $form_state
        ->setValueForElement($element, $parsed_value);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function errorElement(array $element, ConstraintViolationInterface $violation, array $form, FormStateInterface $form_state) {
    $element = parent::errorElement($element, $violation, $form, $form_state);
    return $element === FALSE ? FALSE : $element[$violation->arrayPropertyPath[0]];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
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 2
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.
PluginSettingsBase::$defaultSettingsMerged protected property Whether default settings have been merged into the current $settings.
PluginSettingsBase::$thirdPartySettings protected property The plugin settings injected by third party modules.
PluginSettingsBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies 6
PluginSettingsBase::getSetting public function Returns the value of a setting, or its default value if absent. Overrides PluginSettingsInterface::getSetting
PluginSettingsBase::getSettings public function Returns the array of settings, including defaults for missing settings. Overrides PluginSettingsInterface::getSettings
PluginSettingsBase::getThirdPartyProviders public function Gets the list of third parties that store information. Overrides ThirdPartySettingsInterface::getThirdPartyProviders
PluginSettingsBase::getThirdPartySetting public function Gets the value of a third-party setting. Overrides ThirdPartySettingsInterface::getThirdPartySetting
PluginSettingsBase::getThirdPartySettings public function Gets all third-party settings of a given module. Overrides ThirdPartySettingsInterface::getThirdPartySettings
PluginSettingsBase::mergeDefaults protected function Merges default settings values into $settings.
PluginSettingsBase::onDependencyRemoval public function Informs the plugin that some configuration it depends on will be deleted. Overrides PluginSettingsInterface::onDependencyRemoval 3
PluginSettingsBase::setSetting public function Sets the value of a setting for the plugin. Overrides PluginSettingsInterface::setSetting
PluginSettingsBase::setSettings public function Sets the settings for the plugin. Overrides PluginSettingsInterface::setSettings
PluginSettingsBase::setThirdPartySetting public function Sets the value of a third-party setting. Overrides ThirdPartySettingsInterface::setThirdPartySetting
PluginSettingsBase::unsetThirdPartySetting public function Unsets a third-party setting. Overrides ThirdPartySettingsInterface::unsetThirdPartySetting
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
TimeFieldStandardWidget::$currentUser protected property The current user.
TimeFieldStandardWidget::$deleteDepth protected static property The depth of the delete button.
TimeFieldStandardWidget::$entityBrowserValueUpdated protected property If triggering element was hidden target_id element.
TimeFieldStandardWidget::$entityTypeManager protected property Entity type manager service.
TimeFieldStandardWidget::$fieldDisplayManager protected property Field widget display plugin manager.
TimeFieldStandardWidget::$libraryDiscovery protected property If triggering element was hidden target_id element.
TimeFieldStandardWidget::$moduleHandler protected property The module handler interface.
TimeFieldStandardWidget::create public static function Creates an instance of the plugin. Overrides WidgetBase::create
TimeFieldStandardWidget::defaultSettings public static function Defines the default settings for this plugin. Overrides PluginSettingsBase::defaultSettings
TimeFieldStandardWidget::errorElement public function Assigns a field-level validation error to the right widget sub-element. Overrides WidgetBase::errorElement
TimeFieldStandardWidget::formElement public function Returns the form for a single field widget. Overrides WidgetInterface::formElement
TimeFieldStandardWidget::settingsForm public function Returns a form to configure settings for the widget. Overrides WidgetBase::settingsForm
TimeFieldStandardWidget::settingsSummary public function Returns a short summary for the current widget settings. Overrides WidgetBase::settingsSummary
TimeFieldStandardWidget::validateTimeField public static function Validation Callback; Timefield process field.
TimeFieldStandardWidget::__construct public function Constructs widget plugin. Overrides WidgetBase::__construct
WidgetBase::$fieldDefinition protected property The field definition.
WidgetBase::$settings protected property The widget settings. Overrides PluginSettingsBase::$settings
WidgetBase::addMoreAjax public static function Ajax callback for the "Add another item" button.
WidgetBase::addMoreSubmit public static function Submission handler for the "Add another item" button.
WidgetBase::afterBuild public static function After-build handler for field elements in a form.
WidgetBase::extractFormValues public function Extracts field values from submitted form values. Overrides WidgetBaseInterface::extractFormValues 2
WidgetBase::flagErrors public function Reports field-level validation errors against actual form elements. Overrides WidgetBaseInterface::flagErrors 2
WidgetBase::form public function Creates a form element for a field. Overrides WidgetBaseInterface::form 3
WidgetBase::formMultipleElements protected function Special handling to create form elements for multiple values. 1
WidgetBase::formSingleElement protected function Generates the form element for a single copy of the widget.
WidgetBase::getFieldSetting protected function Returns the value of a field setting.
WidgetBase::getFieldSettings protected function Returns the array of field settings.
WidgetBase::getFilteredDescription protected function Returns the filtered field description.
WidgetBase::getWidgetState public static function Retrieves processing information about the widget from $form_state. Overrides WidgetBaseInterface::getWidgetState
WidgetBase::getWidgetStateParents protected static function Returns the location of processing information within $form_state.
WidgetBase::handlesMultipleValues protected function Returns whether the widget handles multiple values.
WidgetBase::isApplicable public static function Returns if the widget can be used for the provided field. Overrides WidgetInterface::isApplicable 4
WidgetBase::isDefaultValueWidget protected function Returns whether the widget used for default value form.
WidgetBase::massageFormValues public function Massages the form values into the format expected for field values. Overrides WidgetInterface::massageFormValues 7
WidgetBase::setWidgetState public static function Stores processing information about the widget in $form_state. Overrides WidgetBaseInterface::setWidgetState