Allow to alter the values in the widget by contrib/custom modules

Created on 27 September 2018, about 6 years ago
Updated 7 August 2023, over 1 year ago

Problem/Motivation

In a few our projects we use "daterange" field type for "start_date" and "end_date" witout time, because dates, for example, 10-01-2018:10:00 - 10-02-2018:12:00 means the events started 10-01-2018:10:00 and will be finished 10-02-2018:12:00 (office works 24h, in another words). In our cases, the info about time stored using another field provided by Time Field β†’ module. Furthermore, in the custom module time can be overridden with "All Day", so we can NOT use just tokens like [node:field_start_date:value]:[node:field_work_time:from] because can't convert the tokens depends on all cases.

Proposed resolution

Into /addtocalendar/src/Plugin/Field/FieldFormatter/AddToCalendar.php we need to add the couple strings of the code which allow to alter the value of the field.
In the result, the developers can use the following or similar code:

/**
 * Implements hook_addtocalendar_field_alter().
 */
function custommodule_addtocalendar_field_alter(&$value, EntityInterface $entity, $field_name) {
  $value = trim($value);

  switch ($field_name) {
    case 'field_start_date':

      $time = $entity->get('field_work_time')->getValue();
      if (!empty($time) && isset($time[0])) {
        $time = Time::createFromTimestamp($time[0]['from'])
          ->formatForWidget();
        $value .= 'T' . $time;
      }

      break;

    case 'field_end_date':

      $time = $entity->get('field_work_time')->getValue();
      if (!empty($time) && isset($time[0])) {
        $time = Time::createFromTimestamp($time[0]['to'])
          ->formatForWidget();
        $value .= 'T' . $time;
      }

      break;

    default:
      // Nothing to do.
      break;
  }

}

User interface changes

None.

API changes

Will be added addtocalendar.api.php file.

Data model changes

None.

✨ Feature request
Status

Fixed

Version

3.0

Component

Code

Created by

πŸ‡ΊπŸ‡¦Ukraine Ruslan Piskarov Kiev, Ukraine

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States mmenavas

    Unfortunately patch #10 does not work on the recently released version 8.x-3.3.

  • πŸ‡ΊπŸ‡ΈUnited States grgcrlsn321

    Rerolled the patch for latest release 8.x-3.3.

  • Status changed to Needs work over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States danflanagan8 St. Louis, US

    This looks like a useful feature. However, I think there are a few things to address before this could be committed.

    +++ b/src/Plugin/Field/FieldFormatter/AddToCalendar.php
    @@ -209,7 +223,7 @@ class AddToCalendar extends FormatterBase implements ContainerFactoryPluginInter
    -  public function getProperValue(array $field_setting, $entity, array $options = []) {
    +  public function getProperValue($field_setting_name, array $field_setting, $entity, array $options = array()) {
    

    Since this is a public method, I think we'd need to add the new argument as an optional argument at the end of the argument list.

    +++ b/src/Plugin/Field/FieldFormatter/AddToCalendar.php
    @@ -89,11 +98,14 @@ class AddToCalendar extends FormatterBase implements ContainerFactoryPluginInter
    -  public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AddToCalendarApiWidget $add_to_calendar_api_widget, Token $token, RendererInterface $renderer = NULL) {
    +  public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AddToCalendarApiWidget $add_to_calendar_api_widget, Token $token, RendererInterface $renderer = NULL, ModuleHandlerInterface $module_handler) {
    

    If we're adding an argument to the constructor, we should add a BC layer similar to what was just added for the $renderer argument.

    +++ b/src/Plugin/Field/FieldFormatter/AddToCalendar.php
    @@ -243,6 +257,17 @@ class AddToCalendar extends FormatterBase implements ContainerFactoryPluginInter
    +    // @see addtocalendar.api.php
    

    This patch appears to be missing the addtocalendar.api.php file.

    Thanks!

  • πŸ‡ΊπŸ‡ΈUnited States grgcrlsn321

    Really appreciate the input @danflanagan8, I updated the patch with your suggestions and made sure to include the addtocalendar.api.php file.

    Thanks!

  • πŸ‡ΊπŸ‡ΈUnited States grgcrlsn321

    Made a small typo on the last patch file upload. Use this patch for 8.x-3.3 release.

  • Status changed to Fixed over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States danflanagan8 St. Louis, US

    Thanks all! I committed a fix for this. It's a slightly cleaned up version of #15

    Here's the interdiff between 15 and what I committed.

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024