You are here

function smart_date_recur_widget_extra_fields in Smart Date 3.0.x

Same name and namespace in other branches
  1. 8.2 modules/smart_date_recur/smart_date_recur.module \smart_date_recur_widget_extra_fields()
  2. 3.x modules/smart_date_recur/smart_date_recur.module \smart_date_recur_widget_extra_fields()
  3. 3.1.x modules/smart_date_recur/smart_date_recur.module \smart_date_recur_widget_extra_fields()
  4. 3.2.x modules/smart_date_recur/smart_date_recur.module \smart_date_recur_widget_extra_fields()
  5. 3.3.x modules/smart_date_recur/smart_date_recur.module \smart_date_recur_widget_extra_fields()
  6. 3.4.x modules/smart_date_recur/smart_date_recur.module \smart_date_recur_widget_extra_fields()

Helper function to add extra fields to Smart Date widgets.

1 call to smart_date_recur_widget_extra_fields()
SmartDateWidgetBase::formElement in src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php
Returns the form for a single field widget.
2 string references to 'smart_date_recur_widget_extra_fields'
SmartDateWidgetBase::formElement in src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php
Returns the form for a single field widget.
SmartDateWidgetBase::massageFormValues in src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php
Massages the form values into the format expected for field values.

File

modules/smart_date_recur/smart_date_recur.module, line 127
Field hooks for a field that stores a start and end date as timestamps.

Code

function smart_date_recur_widget_extra_fields(&$element, $item, $modal = FALSE) {
  $default_repeat = '';
  $default_end_count = '';
  $default_end_date = '';
  $limit_type = '';
  $defaults = [
    'interval' => NULL,
    'which' => '',
    'day' => '',
    'byday' => [],
  ];

  // Get current user.
  $user = \Drupal::currentUser();

  // Check for permission.
  $recur_permitted = $user
    ->hasPermission('make smart dates recur');
  if ($item->rrule) {
    $rrule = SmartDateRule::load($item->rrule);
    if ($rrule) {
      $default_repeat = $rrule
        ->get('freq')
        ->getString();
      if ($rrule
        ->get('limit')
        ->getString()) {
        list($limit_type, $limit_value) = explode('=', $rrule
          ->get('limit')
          ->getString());
        if ($limit_type == 'COUNT') {
          $default_end_count = $limit_value;
        }
        elseif ($limit_type == 'UNTIL') {
          $default_end_date = $limit_value;
        }
      }
      $defaults = $rrule
        ->getParametersArray();
    }
    $element['rrule'] = [
      '#type' => 'hidden',
      '#title' => t('Existing Rule ID'),
      '#value' => $item->rrule,
    ];
    if (!$recur_permitted) {
      $element['repeat'] = [
        '#type' => 'hidden',
        '#title' => t('Existing repeat frequency'),
        '#value' => $default_repeat,
      ];
      $element['repeat-end'] = [
        '#type' => 'hidden',
        '#title' => t('Existing repeat limit type'),
        '#value' => $limit_type,
      ];
      $element['repeat-end-count'] = [
        '#type' => 'hidden',
        '#title' => t('Existing maximum instances'),
        '#value' => $default_end_count,
      ];
      $element['repeat-end-date'] = [
        '#type' => 'hidden',
        '#title' => t('Existing last instance date'),
        '#value' => $default_end_date,
      ];
      if ($rule_text = $rrule
        ->getTextRule()) {
        $element['repeat-text'] = [
          '#markup' => '<span class="clearfix"></span>
            <h4 class="label">' . t('Repeats') . '</h4>
            <p class="repeat-text"> ' . $rule_text . '</p>',
        ];
      }
      return;
    }

    // Also insert a link to the interface for managing interfaces.
    $url = Url::fromRoute('smart_date_recur.instances', [
      'rrule' => $item->rrule,
      'modal' => $modal,
    ]);
    $instances_link = Link::fromTextAndUrl(t('Manage Instances'), $url);
    $instances_link = $instances_link
      ->toRenderable();

    // Add some classes.
    $instances_link['#attributes'] = [
      'class' => [
        'button',
        'button--small',
        'manage-instances',
      ],
    ];
    if ($modal) {
      $instances_link['#attributes']['class'][] = 'use-ajax';
      $instances_link['#attached']['library'][] = 'core/drupal.dialog.ajax';
    }
    $instances_link['#weight'] = 100;
    $element['manage-instances'] = $instances_link;
  }
  elseif (!$recur_permitted) {
    return;
  }
  $select_repeat = 'select[name$="[' . $element['#delta'] . '][repeat]"]';
  $select_repeat_end = 'select[name$="[' . $element['#delta'] . '][repeat-end]"]';
  $element['repeat'] = [
    '#type' => 'select',
    '#title' => t('Repeats'),
    '#prefix' => '<div class="clearfix"></div>',
    '#options' => [
      '' => t('None'),
      'DAILY' => t('Daily'),
      'WEEKLY' => t('Weekly'),
      'MONTHLY' => t('Monthly'),
      'YEARLY' => t('Annually'),
    ],
    '#default_value' => $default_repeat,
    '#attributes' => [
      'class' => [
        'recur-repeat',
      ],
    ],
  ];
  $element['repeat-end'] = [
    '#type' => 'select',
    '#title' => t('Ends'),
    '#label_attributes' => [
      'class' => [
        'pad-left',
      ],
    ],
    '#options' => [
      '' => t('Never'),
      'COUNT' => t('After'),
      'UNTIL' => t('On Date'),
    ],
    '#states' => [
      // Show this textarea only if the 'repeat' select has a value.
      'invisible' => [
        $select_repeat => [
          'value' => '',
        ],
      ],
    ],
    '#default_value' => $limit_type,
  ];
  $element['repeat-end-count'] = [
    '#type' => 'number',
    '#title' => t('Ends after'),
    '#title_display' => t('invisible'),
    '#min' => 2,
    '#step' => 1,
    '#field_suffix' => t('times'),
    '#placeholder' => t('# of'),
    '#states' => [
      // Show this textarea only if the 'repeat' select has a value.
      'visible' => [
        $select_repeat => [
          '!value' => '',
        ],
        $select_repeat_end => [
          'value' => 'COUNT',
        ],
      ],
    ],
    '#default_value' => $default_end_count,
  ];
  $element['repeat-end-date'] = [
    '#type' => 'date',
    '#title' => t('Ends on date'),
    '#title_display' => t('invisible'),
    '#states' => [
      // Show this textarea only if the 'repeat' select has a value.
      'visible' => [
        $select_repeat => [
          '!value' => '',
        ],
        $select_repeat_end => [
          'value' => 'UNTIL',
        ],
      ],
    ],
    '#default_value' => $default_end_date,
    '#attributes' => [
      'class' => [
        'repeat-end-date',
      ],
      'type' => 'date',
    ],
  ];
  $element['repeat-advanced'] = [
    '#type' => 'details',
    '#title' => t('Advanced'),
    '#open' => $defaults['interval'] || $defaults['which'] || $defaults['day'] || $defaults['byday'],
    '#states' => [
      // Show this textarea only if the 'repeat' select has a value.
      'invisible' => [
        [
          $select_repeat => [
            'value' => '',
          ],
        ],
      ],
    ],
  ];
  $element['repeat-advanced']['interval'] = [
    '#type' => 'number',
    '#title' => t('Every'),
    '#label_attributes' => [
      'class' => [
        'field-interval--label',
      ],
    ],
    '#attributes' => [
      'class' => [
        'field-interval',
      ],
    ],
    '#min' => 1,
    '#step' => 1,
    '#field_suffix' => t('times'),
    '#placeholder' => t('# of'),
    '#default_value' => $defaults['interval'],
  ];

  // Checkboxes to select which weekdays for weekly repeats.
  $days = [
    'SU' => t('Sunday'),
    'MO' => t('Monday'),
    'TU' => t('Tuesday'),
    'WE' => t('Wednesday'),
    'TH' => t('Thursday'),
    'FR' => t('Friday'),
    'SA' => t('Saturday'),
  ];

  // Weekday int. 0-6 (Sun-Sat).
  $firstDayInt = \Drupal::config('system.date')
    ->get('first_day');

  // Rebuild weekday options where system first day is first option in list.
  $days_by_config = array_merge(array_slice($days, $firstDayInt), array_slice($days, 0, $firstDayInt));
  $element['repeat-advanced']['byday'] = [
    '#type' => 'checkboxes',
    '#title' => t('on days'),
    '#title_display' => t('inline'),
    '#options' => $days_by_config,
    // Populate with any existing values.
    '#default_value' => $defaults['byday'],
    '#states' => [
      // Show only if the repeat select has an appropriate value.
      'visible' => [
        [
          $select_repeat => [
            'value' => 'WEEKLY',
          ],
        ],
      ],
    ],
    '#attributes' => [
      'class' => [
        'container-inline',
        'byday-checkboxes',
      ],
    ],
  ];
  $element['repeat-advanced']['which'] = [
    '#type' => 'select',
    '#title' => t('on the'),
    '#options' => [
      '' => t('- Select -'),
      '1' => t('First'),
      '2' => t('Second'),
      '3' => t('Third'),
      '4' => t('Fourth'),
      '5' => t('Fifth'),
      '-1' => t('Last'),
    ],
    '#default_value' => $defaults['which'],
    '#states' => [
      // Show this textarea only if the repeat select has an appropriate value.
      'invisible' => [
        [
          $select_repeat => [
            'value' => 'WEEKLY',
          ],
        ],
        [
          $select_repeat => [
            'value' => 'DAILY',
          ],
        ],
      ],
    ],
  ];
  $element['repeat-advanced']['weekday'] = [
    '#type' => 'select',
    '#title' => t('Weekday'),
    '#title_display' => t('invisible'),
    '#label_attributes' => [
      'class' => [
        'pad-left',
      ],
    ],
    '#options' => [
      '' => t('- Day (any) -'),
      'SU' => t('Sunday'),
      'MO' => t('Monday'),
      'TU' => t('Tuesday'),
      'WE' => t('Wednesday'),
      'TH' => t('Thursday'),
      'FR' => t('Friday'),
      'SA' => t('Saturday'),
      'MO,TU,WE,TH,FR' => t('Weekday'),
      'SA,SU' => t('Weekend day'),
    ],
    '#default_value' => $defaults['day'],
    '#states' => [
      // Show this textarea only if the repeat select has an appropriate value.
      'invisible' => [
        [
          $select_repeat => [
            'value' => 'WEEKLY',
          ],
        ],
        [
          $select_repeat => [
            'value' => 'DAILY',
          ],
        ],
      ],
    ],
  ];
}