You are here

public function ScheduleTask::getSettingsForm in Business Rules 2.x

Same name and namespace in other branches
  1. 8 src/Plugin/BusinessRulesAction/ScheduleTask.php \Drupal\business_rules\Plugin\BusinessRulesAction\ScheduleTask::getSettingsForm()

Return the form array.

@internal param array $form

Parameters

array $form: The form array.

\Drupal\Core\Form\FormStateInterface $form_state: The form state object.

\Drupal\business_rules\ItemInterface $item: The configured item.

Return value

array The render array for the settings form.

Overrides BusinessRulesItemPluginBase::getSettingsForm

File

src/Plugin/BusinessRulesAction/ScheduleTask.php, line 69

Class

ScheduleTask
Class ScheduleTask.

Namespace

Drupal\business_rules\Plugin\BusinessRulesAction

Code

public function getSettingsForm(array &$form, FormStateInterface $form_state, ItemInterface $item) {

  // Only show settings form if the item is already saved.
  if ($item
    ->isNew()) {
    return [];
  }
  $settings['time_offset'] = [];
  $settings['time_unit'] = [];
  $settings['identifier'] = [
    '#type' => 'textfield',
    '#title' => t('Identifier'),
    '#description' => t('Use this string to identity the task. Any existing task for this action will be replaced. You can use tokens or variables to make this field unique.'),
    '#required' => TRUE,
    '#default_value' => $item
      ->getSettings('identifier'),
  ];
  $settings['scheduler'] = [
    '#type' => 'fieldset',
    '#title' => t('Scheduler'),
    '#description' => t('How much time before or after the date field above do you want to trigger the schedule?'),
    '#attributes' => [
      'class' => [
        'display-flex',
        'fieldgroup',
      ],
    ],
  ];
  $settings['scheduler']['time_offset'] = [
    '#type' => 'textfield',
    '#title' => t('Time offset'),
    '#default_value' => $item
      ->getSettings('time_offset'),
    '#required' => TRUE,
    '#size' => 8,
    '#prefix' => '<div class="padding-right-20">',
    '#suffix' => '</div>',
  ];
  $settings['scheduler']['time_unit'] = [
    '#type' => 'select',
    '#title' => t('Time Unit'),
    '#options' => $this->timeUnitOptions,
    '#default_value' => $item
      ->getSettings('time_unit'),
    '#required' => TRUE,
    '#prefix' => '<div>',
    '#suffix' => '</div>',
  ];

  // Filter fields option to present only date time and timestam options.
  $fields =& $form['settings']['field']['#options'];
  foreach ($fields as $key => $field) {
    $type = $field
      ->getArguments()['@type'];
    if (!in_array($type, [
      'changed',
      'created',
      'timestamp',
      'datetime',
    ])) {
      unset($fields[$key]);
    }
  }
  $settings['update_entity'] = [
    '#type' => 'checkbox',
    '#title' => t('Save entity as the last action of the task.'),
    '#description' => t('Check this option if you are changing values on the entity and you want to persist those changes on the database.'),
    '#default_value' => $item
      ->getSettings('update_entity'),
  ];
  $form['settings']['field']['#description'] = t('Entity changed, created, timestamp or datetime field.');

  // The items to process.
  $settings['items'] = [
    '#type' => 'details',
    '#description' => t('The items are evaluated on the presented order. Drag them to change the order.'),
    '#title' => t('Items'),
    '#open' => TRUE,
  ];
  $this->item = $item;
  $settings['items'][] = $this
    ->formItems($form, $form_state, $item);
  return $settings;
}