You are here

public function EventTypeForm::buildForm in RNG - Events and Registrations 3.x

Same name and namespace in other branches
  1. 8.2 src/Form/EventTypeForm.php \Drupal\rng\Form\EventTypeForm::buildForm()
  2. 8 src/Form/EventTypeForm.php \Drupal\rng\Form\EventTypeForm::buildForm()

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 EntityForm::buildForm

File

src/Form/EventTypeForm.php, line 108

Class

EventTypeForm
Form controller for event config entities.

Namespace

Drupal\rng\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildForm($form, $form_state);

  /** @var \Drupal\rng\Entity\EventTypeInterface $event_type */
  $event_type = $this->entity;
  if (!$event_type
    ->isNew()) {
    $form['#title'] = $this
      ->t('Edit event type %label configuration', [
      '%label' => $event_type
        ->label(),
    ]);
  }
  if ($event_type
    ->isNew()) {
    $bundle_options = [];

    // Generate a list of fieldable bundles which are not events.
    foreach ($this->entityManager
      ->getDefinitions() as $entity_type) {
      if ($entity_type
        ->entityClassImplements('\\Drupal\\Core\\Entity\\ContentEntityInterface')) {
        foreach ($this->bundleInfo
          ->getBundleInfo($entity_type
          ->id()) as $bundle => $bundle_info) {
          if (!$this->eventManager
            ->eventType($entity_type
            ->id(), $bundle)) {
            $bundle_options[(string) $entity_type
              ->getLabel()][$entity_type
              ->id() . '.' . $bundle] = $bundle_info['label'];
          }
        }
      }
    }
    if ($this->moduleHandler
      ->moduleExists('node')) {
      $form['#attached']['library'][] = 'rng/rng.admin';
      $form['entity_type'] = [
        '#type' => 'radios',
        '#options' => NULL,
        '#title' => $this
          ->t('Event entity type'),
        '#required' => TRUE,
        // Kills \Drupal\Core\Render\Element\Radios::processRadios.
        '#process' => [],
      ];
      $form['entity_type']['node']['radio'] = [
        '#type' => 'radio',
        '#title' => $this
          ->t('Create a new content type'),
        '#description' => $this
          ->t('Create a content type to use as an event type.'),
        '#return_value' => "node",
        '#parents' => [
          'entity_type',
        ],
        '#default_value' => 'node',
      ];
      $form['entity_type']['existing']['radio'] = [
        '#type' => 'radio',
        '#title' => $this
          ->t('Use existing bundle'),
        '#description' => $this
          ->t('Use an existing entity/bundle combination.'),
        '#return_value' => "existing",
        '#parents' => [
          'entity_type',
        ],
        '#default_value' => '',
      ];
      $form['entity_type']['existing']['container'] = [
        '#type' => 'container',
        '#attributes' => [
          'class' => [
            'rng-radio-indent',
          ],
        ],
      ];
    }
    $form['entity_type']['existing']['container']['bundle'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Bundle'),
      '#options' => $bundle_options,
      '#default_value' => $event_type
        ->id(),
      '#disabled' => !$event_type
        ->isNew(),
      '#empty_option' => $bundle_options ? NULL : t('No Bundles Available'),
    ];
  }
  $form['settings'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Settings'),
  ];

  // Mirror permission.
  $form['access']['mirror_update'] = [
    '#group' => 'settings',
    '#type' => 'checkbox',
    '#title' => t('Mirror manage registrations with update permission'),
    '#description' => t('Allow users to <strong>manage registrations</strong> if they have <strong>update</strong> permission on an event entity.'),
    '#default_value' => (bool) ($event_type
      ->getEventManageOperation() !== NULL ? $event_type
      ->getEventManageOperation() : TRUE),
  ];

  // Allow Anonymous Registrants?
  $form['allow_anon_registrants'] = [
    '#group' => 'settings',
    '#type' => 'checkbox',
    '#title' => t('Allow anonymous registrants without saving to other identities?'),
    '#description' => t('Allow editing registrant data as text fields on a registration. Add fields to the registrant type for information you would like to collect.'),
    '#default_value' => (bool) $event_type
      ->getAllowAnonRegistrants(),
  ];

  // Auto Sync Registrants
  $form['auto_sync_registrants'] = [
    '#group' => 'settings',
    '#type' => 'checkbox',
    '#title' => t('Sync matching field data between registrant and registrant identity'),
    '#description' => t('If there are empty fields on either the registrant or an identity, copy data from the other when a registrant is updated. This can streamline views of attendee data.'),
    '#default_value' => (bool) $event_type
      ->getAutoSyncRegistrants(),
  ];

  // Auto Attach User Identities
  $form['auto_attach_users'] = [
    '#group' => 'settings',
    '#type' => 'checkbox',
    '#title' => t('Automatically add user identities to anonymous registrants if email matches'),
    '#description' => t('If an email field in a registrant matches a user account, automatically add the user account as the identity.'),
    '#default_value' => (bool) $event_type
      ->getAutoAttachUsers(),
    '#states' => [
      'invisible' => [
        ':input[name="allow_anon_registrants"]' => [
          'checked' => FALSE,
        ],
      ],
    ],
  ];

  // Registrant email field
  $form['registrant_email_field'] = [
    '#group' => 'settings',
    '#type' => 'textfield',
    '#title' => t('Registrant email field'),
    '#description' => t('Machine name of a field on the registrant to use when looking up a user account.'),
    '#default_value' => $event_type
      ->getRegistrantEmailField(),
    '#states' => [
      'invisible' => [
        ':input[name="auto_attach_users"]' => [
          'checked' => FALSE,
        ],
      ],
    ],
  ];

  // Event date fields
  $form['event_date_field_start'] = [
    '#group' => 'settings',
    '#type' => 'textfield',
    '#title' => t('Event Start Date field'),
    '#description' => t('Machine name of a field on the event to use as the event start date.'),
    '#default_value' => $event_type
      ->getEventStartDateField(),
  ];
  $form['event_date_field_end'] = [
    '#group' => 'settings',
    '#type' => 'textfield',
    '#title' => t('Event End Date field'),
    '#description' => t('Machine name of a field on the event to use as the event end date. Will be combined into a "friendly" date string on registrations.'),
    '#default_value' => $event_type
      ->getEventEndDateField(),
  ];
  $registrant_types = [];
  foreach (RegistrantType::loadMultiple() as $registrant_type) {

    /** @var \Drupal\rng\Entity\RegistrantTypeInterface $registrant_type */
    $registrant_types[$registrant_type
      ->id()] = $registrant_type
      ->label();
  }
  $form['registrants'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Registrants'),
    '#tree' => TRUE,
  ];

  // Default registrant type.
  $form['registrants']['registrant_type'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Default registrant type'),
    '#description' => $this
      ->t('Registrant type used for new registrants associated with this event type.'),
    '#required' => TRUE,
    '#options' => $registrant_types,
    '#default_value' => $event_type
      ->getDefaultRegistrantType(),
  ];
  $form['registrants']['registrants'] = [
    '#type' => 'table',
    '#header' => [
      [
        'data' => $this
          ->t('Person type'),
      ],
      [
        'data' => $this
          ->t('Permit inline creation of entities'),
        'class' => [
          'checkbox',
        ],
      ],
      [
        'data' => $this
          ->t('Form display mode'),
      ],
      [
        'data' => $this
          ->t('Permit referencing existing entities'),
        'class' => [
          'checkbox',
        ],
      ],
    ],
    '#empty' => $this
      ->t('There are no people types available.'),
  ];
  foreach ($this->rngConfiguration
    ->getIdentityTypes() as $entity_type_id) {
    $entity_type = $this->entityTypeManager
      ->getDefinition($entity_type_id);
    $bundles = $this->bundleInfo
      ->getBundleInfo($entity_type_id);
    foreach ($bundles as $bundle => $info) {
      $t_args = [
        '@bundle' => $info['label'],
        '@entity_type' => $entity_type
          ->getLabel(),
      ];
      $row = [];
      $row['people_type']['#markup'] = $this
        ->t('@bundle (@entity_type)', $t_args);
      $row['create'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Permit inline creation of @bundle entities', $t_args),
        '#title_display' => 'invisible',
        '#default_value' => $event_type
          ->canIdentityTypeCreate($entity_type_id, $bundle),
        '#wrapper_attributes' => [
          'class' => [
            'checkbox',
          ],
        ],
      ];
      $row['entity_form_mode'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Form display mode used when the entity is created inline.', $t_args),
        '#title_display' => 'invisible',
        '#default_value' => $event_type
          ->getIdentityTypeEntityFormMode($entity_type_id, $bundle),
        '#options' => $this->entityDisplayRepository
          ->getFormModeOptionsByBundle($entity_type_id, $bundle),
      ];
      $row['existing'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Permit referencing existing @bundle entities', $t_args),
        '#title_display' => 'invisible',
        '#default_value' => $event_type
          ->canIdentityTypeReference($entity_type_id, $bundle),
        '#wrapper_attributes' => [
          'class' => [
            'checkbox',
          ],
        ],
      ];
      $row_key = "{$entity_type_id}:{$bundle}";
      $form['registrants']['registrants'][$row_key] = $row;
    }
  }

  // Check if user people type is enabled, then apply some special handling.
  if (isset($form['registrants']['registrants']['user:user'])) {

    // Blacklist user creation. It does not work because it is special.
    $form['registrants']['registrants']['user:user']['create']['#access'] = FALSE;

    // Auto check existing references for users.
    if ($event_type
      ->isNew()) {
      $form['registrants']['registrants']['user:user']['existing']['#default_value'] = TRUE;
    }
  }
  return $form;
}