You are here

public function HeartbeatTypeForm::form in Heartbeat 8

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/Form/HeartbeatTypeForm.php, line 92

Class

HeartbeatTypeForm
Class HeartbeatTypeForm.

Namespace

Drupal\heartbeat\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form_state
    ->setCached(FALSE);
  $heartbeat_type = $this->entity;
  $form['#tree'] = TRUE;
  $form['#attached']['library'] = 'heartbeat/treeTable';
  $form['label'] = array(
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#maxlength' => 255,
    '#default_value' => $heartbeat_type
      ->label(),
    '#description' => $this
      ->t("Label for the Heartbeat Type."),
    '#required' => TRUE,
  );
  $form['description'] = array(
    '#type' => 'textfield',
    '#title' => $this
      ->t('description'),
    '#maxlength' => 255,
    '#default_value' => $heartbeat_type
      ->getDescription(),
    '#description' => $this
      ->t("Description of the Heartbeat Type"),
    '#required' => TRUE,
  );

  //TODO Select is nested, thus any returned value seems to be read as "true", which results in the first item within the nest being chosen as the value populating the form's field. This will need to be fixed to prevent wrongly changing one's values without thinking about it.
  $form['entity_type'] = array(
    '#type' => 'select',
    '#title' => $this
      ->t('Entity Type'),
    //      '#default_value' => $heartbeat_type->getMainEntity(),
    '#description' => $this
      ->t("Primary Entity Type for this Heartbeat Type"),
    '#options' => array(
      $this->entityTypes,
    ),
    '#required' => TRUE,
  );
  $bundles = $form_state
    ->get('entity_bundles');
  $form['entity_bundles'] = array(
    '#type' => 'container',
    '#prefix' => '<div id="entity-bundles">',
    '#suffix' => '</div>',
  );
  $form['entity_bundles']['getBundles'] = [
    '#type' => 'submit',
    '#value' => t('Getting bundles'),
    '#submit' => array(
      '::getBundlesForEntity',
    ),
    '#ajax' => [
      'callback' => '::getBundlesForEntity',
      'wrapper' => 'entity-bundles',
      'progress' => array(
        'type' => 'throbber',
        'message' => t('Getting bundles'),
      ),
    ],
  ];
  $form['entity_bundles']['list'] = array(
    '#type' => 'select',
    '#title' => $this
      ->t('Entity Bundles'),
    '#default_value' => $heartbeat_type
      ->getBundle(),
    '#description' => $this
      ->t("Any bundles available to the specified entity"),
    '#options' => $bundles,
  );
  $form['message'] = array(
    '#type' => 'textfield',
    '#title' => $this
      ->t('message'),
    '#maxlength' => 255,
    '#default_value' => $heartbeat_type
      ->getMessage(),
    '#description' => $this
      ->t("The structure for messages of this type. Use !exclamation marks before fields and entities"),
    '#required' => TRUE,
  );

  //    $form['message_concat'] = array(
  //      '#type' => 'textfield',
  //      '#title' => $this->t('Message structure in concatenated form'),
  //      '#maxlength' => 255,
  //      '#default_value' => $heartbeat_type->getMessageConcat(),
  //      '#description' => $this->t("The structure for messages of this type. Use !exclamation marks before fields and entities"),
  //      '#required' => FALSE,
  //    );
  $form['perms'] = array(
    '#type' => 'select',
    '#title' => $this
      ->t('Permissions'),
    '#default_value' => $heartbeat_type
      ->getPerms(),
    '#description' => $this
      ->t("Default permissions to view Heartbeats of this type"),
    '#options' => array(
      Entity\HEARTBEAT_NONE => 'None',
      Entity\HEARTBEAT_PRIVATE => 'Private',
      Entity\HEARTBEAT_PUBLIC_TO_ADDRESSEE => 'Public to Addressee',
      Entity\HEARTBEAT_PUBLIC_TO_CONNECTED => 'Public to Connected',
      Entity\HEARTBEAT_PUBLIC_TO_ALL => 'Public to All',
    ),
    '#required' => TRUE,
  );
  $form['group_type'] = array(
    '#type' => 'select',
    '#title' => $this
      ->t('Group Type'),
    '#default_value' => 0,
    '#description' => $this
      ->t("Type of group associated with Heartbeats of this type"),
    '#options' => array(
      Entity\HEARTBEAT_GROUP_NONE => 'None',
      Entity\HEARTBEAT_GROUP_SINGLE => 'Single',
      Entity\HEARTBEAT_GROUP_SUMMARY => 'Group',
    ),
    '#required' => TRUE,
  );
  $form['variables'] = array(
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Variables to map'),
    '#prefix' => '<div id="Variables-fieldset-wrapper">',
    '#suffix' => '</div>',
  );
  $messageArguments = $form_state
    ->get('data_hidden');
  if ($messageArguments === NULL) {
    $messageArguments = $this
      ->extractMessageArguments($heartbeat_type
      ->getMessage());
  }
  $argNum = count($messageArguments);
  for ($i = 0; $i < $argNum; $i++) {
    if (is_array($messageArguments) && $messageArguments[$i] != null) {
      $variableValue = isset($heartbeat_type
        ->getVariables()[$i]) && !empty($heartbeat_type
        ->getVariables()[$i]) ? $heartbeat_type
        ->getVariables()[$i] : '';
      $form['variables'][$i] = array(
        '#type' => 'textfield',
        '#title' => t($messageArguments[$i]),
        '#description' => t('Map value to this variable'),
        '#default_value' => $variableValue,
      );
    }
  }
  $form['variables']['rebuildArgs'] = [
    '#type' => 'submit',
    '#value' => t('Rebuild Arguments'),
    '#submit' => array(
      '::rebuildMessageArguments',
    ),
    '#ajax' => [
      'callback' => '::rebuildMessageArguments',
      'wrapper' => 'Variables-fieldset-wrapper',
    ],
  ];
  $form['tokens'] = array(
    '#prefix' => '<div id="token-tree"></div>',
    '#markup' => $this->tokenTree,
  );
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $heartbeat_type
      ->id(),
    '#machine_name' => [
      'exists' => '\\Drupal\\heartbeat\\Entity\\HeartbeatType::load',
    ],
    '#disabled' => !$heartbeat_type
      ->isNew(),
  ];
  $form_state
    ->setCached(FALSE);
  return parent::form($form, $form_state, $heartbeat_type);
}