You are here

public function ConfigureActivityForm::buildForm in Activity 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides MultiStepFormBase::buildForm

File

src/Form/ConfigureActivityForm.php, line 156

Class

ConfigureActivityForm
Configure activities form.

Namespace

Drupal\activity\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {

  // Default value for Activity window field.
  $windowDefault = '';

  // Default value for roles field.
  $rolesDefault = '';

  // Default value for content types field.
  $typesDefault = '';

  // Default value for message field.
  $messageDefault = '';
  $form = parent::buildForm($form, $form_state);
  $current_path = \Drupal::service('path.current')
    ->getPath();
  $result = \Drupal::service('path.alias_manager')
    ->getAliasByPath($current_path);
  $path_args = explode('/', $result);

  // The event id.
  $this->eventId = $path_args[4];

  // If event exists, get label form database.
  if ($this->eventId != 'new') {
    $query = $this->activityService
      ->getActivityEventField($this->eventId, 'label');
    $this->label = $query[0]->label;
    $query = $this->activityService
      ->getActivityEventField($this->eventId, 'hook');
    $this->hook = $query[0]->hook;
  }

  // Get fields values if they exist.
  $messageJson = $this->activityService
    ->getActivityEventField($this->eventId, 'message');
  if (!empty($messageJson)) {
    $activityMessage = json_decode($messageJson[0]->message);
    $windowDefault = $activityMessage->window;
    $rolesDefault = $activityMessage->roles;
    $typesDefault = $activityMessage->types;
    $messageDefault = $activityMessage->message;
  }

  // Event name.
  $form['activity_label'] = [
    '#type' => 'textfield',
    '#title' => t('Label'),
    '#default_value' => $this->label,
    '#required' => TRUE,
    '#size' => 30,
    '#attributes' => [
      'class' => [
        'activity_label',
      ],
    ],
  ];

  // Options for activity window.
  $timeIntervals = range(0, 7200, 300);
  $options = $this
    ->buildOptions($timeIntervals);
  $options[0] = 'Unlimited';
  $form['activity_window'] = [
    '#type' => 'select',
    '#title' => t('Activity Window'),
    '#description' => t('Prevent repeat Activity from the same user for the same entity within this interval'),
    '#options' => $options,
    '#default_value' => $windowDefault == '' ? $this->options['window'] : $windowDefault,
  ];

  // Add roles or content types options based on hook.
  // Do not need content types when need to update user for example.
  if (strpos($this->hook, 'user') !== FALSE) {
    $roles = Role::loadMultiple();
    $roleOptions = [];
    foreach ($roles as $role => $value) {
      $roleOptions[$role] = $role;
    }
    $form['activity_roles'] = [
      '#type' => 'checkboxes',
      '#title' => t('Roles'),
      '#default_value' => $rolesDefault,
      '#options' => $roleOptions,
    ];
  }
  else {
    $bundles = $this->entityTypeManager
      ->getStorage('node_type')
      ->loadMultiple();
    $contentTypes = array_keys($bundles);
    $types = array_combine($contentTypes, $contentTypes);
    $form['activity_node_types'] = [
      '#type' => 'checkboxes',
      '#title' => t('Allowed Node Types'),
      '#default_value' => $typesDefault,
      '#options' => $types,
    ];
  }

  // Message that keeps all the options.
  $form['activity_message'] = [
    '#type' => 'textarea',
    '#title' => t('Public Message'),
    '#description' => t('Message displayed to everyone who is not part of this Activity.'),
    '#default_value' => $messageDefault,
  ];

  // The token.module provides the UI for the tokens when module enabled.
  $moduleHandler = $this->moduleHandler;
  if ($moduleHandler
    ->moduleExists('token')) {

    // Get tokens options.
    $token_tree = $this->treeBuilder
      ->buildAllRenderable([
      'click_insert' => TRUE,
      'show_restricted' => TRUE,
      'show_nested' => FALSE,
    ]);

    // Interest only on these types.
    $tokenFor = [
      'current-date' => 'current-date',
      'current-page' => 'current-page',
      'current-user' => 'current-user',
      'node' => 'node',
      'user' => 'user',
      'random' => 'random',
      'site' => 'site',
    ];
    if (strpos($this->hook, 'user') !== FALSE) {
      unset($tokenFor['node']);
    }
    elseif (strpos($this->hook, 'comment') !== FALSE) {
      $tokenFor['comment'] = 'comment';
    }
    foreach ($token_tree['#token_tree'] as $key => $value) {
      if (!in_array($key, $tokenFor)) {
        unset($token_tree['#token_tree'][$key]);
      }
    }
    $form['token_help'] = [
      '#type' => 'markup',
      '#markup' => \Drupal::service('renderer')
        ->render($token_tree),
    ];
  }
  $form['submit'] = [
    '#type' => 'submit',
    '#value' => t('Save'),
  ];
  return $form;
}