You are here

public function MandrillActivityForm::form in Mandrill 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

modules/mandrill_activity/src/Form/MandrillActivityForm.php, line 24
Contains \Drupal\mandrill_activity\Form\MandrillActivityForm.

Class

MandrillActivityForm
Form controller for the MandrillActivity entity edit form.

Namespace

Drupal\mandrill_activity\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);
  $user_input = $form_state
    ->getUserInput();

  /* @var $activity \Drupal\mandrill_activity\Entity\MandrillActivity */
  $activity = $this->entity;
  $entity_not_null = !empty($activity->id);
  $form['label'] = array(
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#default_value' => $activity->label,
    '#description' => t('The human-readable name of this Mandrill Activity entity.'),
    '#required' => TRUE,
  );
  $form['id'] = array(
    '#type' => 'machine_name',
    '#default_value' => $activity->id,
    '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
    '#machine_name' => array(
      'source' => array(
        'label',
      ),
      'exists' => array(
        $this,
        'exists',
      ),
    ),
    '#description' => t('A unique machine-readable name for this Mandrill Activity entity. It must only contain lowercase letters, numbers, and underscores.'),
    '#disabled' => !$activity
      ->isNew(),
  );
  $form['drupal_entity'] = array(
    '#title' => t('Drupal entity'),
    '#type' => 'fieldset',
    '#attributes' => array(
      'id' => array(
        'Mandrill-activity-drupal-entity',
      ),
    ),
    '#prefix' => '<div id="entity-wrapper">',
    '#suffix' => '</div>',
  );

  // Prep the entity type list before creating the form item:
  $entity_info = \Drupal::entityTypeManager()
    ->getDefinitions();
  $entity_types = array(
    '' => t('-- Select --'),
  );

  /* @var $entity_type \Drupal\Core\Entity\EntityType */
  foreach ($entity_info as $key => $entity_type) {

    // Ignore Mandrill entity types.
    if (strpos($entity_type
      ->id(), 'mandrill') !== FALSE) {
      continue;
    }

    // Ignore configuration entities.
    if (get_class($entity_type) == 'Drupal\\Core\\Config\\Entity\\ConfigEntityType') {
      continue;
    }
    $entity_types[$entity_type
      ->id()] = $entity_type
      ->getLabel();
  }
  asort($entity_types);
  $form['drupal_entity']['entity_type'] = array(
    '#title' => t('Entity type'),
    '#type' => 'select',
    '#options' => $entity_types,
    '#default_value' => $activity->entity_type,
    '#required' => TRUE,
    '#description' => t('Select an entity type to enable Mandrill history on.'),
    '#ajax' => array(
      'callback' => '::entity_callback',
      'wrapper' => 'entity-wrapper',
      'method' => 'replace',
      'effect' => 'fade',
      'progress' => array(
        'type' => 'throbber',
        'message' => t('Retrieving bundles for this entity type.'),
      ),
    ),
  );
  $form_entity_type = isset($user_input['entity_type']) ? $user_input['entity_type'] : NULL;
  if (empty($form_entity_type) && $entity_not_null) {
    $form_entity_type = $activity->entity_type;
  }
  if (!empty($form_entity_type)) {

    // Prep the bundle list before creating the form item.
    $bundle_info = \Drupal::service('entity_type.bundle.info')
      ->getBundleInfo($form_entity_type);
    $bundles = array(
      '' => t('-- Select --'),
    );
    foreach ($bundle_info as $key => $bundle) {
      $bundles[$key] = ucfirst($key);
    }
    asort($bundles);
    $form['drupal_entity']['bundle'] = array(
      '#title' => t('Entity bundle'),
      '#type' => 'select',
      '#required' => TRUE,
      '#description' => t('Select a Drupal entity bundle with an email address to report on.'),
      '#options' => $bundles,
      '#default_value' => $activity->bundle,
      '#ajax' => array(
        'callback' => '::entity_callback',
        'wrapper' => 'entity-wrapper',
        'method' => 'replace',
        'effect' => 'fade',
        'progress' => array(
          'type' => 'throbber',
          'message' => t('Retrieving email fields for this entity type.'),
        ),
      ),
    );
    $form_bundle = isset($user_input['bundle']) ? $user_input['bundle'] : NULL;
    if (empty($form_bundle) && $entity_not_null) {
      $form_bundle = $activity->bundle;
    }
    if (!empty($form_bundle)) {
      $fields = $this
        ->fieldmapOptions($form_entity_type, $form_bundle);
      $form['drupal_entity']['email_property'] = array(
        '#title' => t('Email Property'),
        '#type' => 'select',
        '#required' => TRUE,
        '#description' => t('Select the field which contains the email address'),
        '#options' => $fields,
        '#default_value' => $activity->email_property,
      );
    }
  }
  $form['enabled'] = array(
    '#title' => t('Enabled'),
    '#type' => 'checkbox',
    '#default_value' => $entity_not_null ? $activity->enabled : TRUE,
  );
  return $form;
}