You are here

public static function EventTypeFieldMappingForm::createField in RNG - Events and Registrations 3.x

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

Form submission function to respond to the create field button.

Parameters

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

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

File

src/Form/EventTypeFieldMappingForm.php, line 88

Class

EventTypeFieldMappingForm
Form for event type field mapping.

Namespace

Drupal\rng\Form

Code

public static function createField(array $form, FormStateInterface $form_state) {

  /** @var \Drupal\rng\Entity\EventTypeInterface $event_type */
  $event_type = $form_state
    ->getFormObject()
    ->getEntity();
  $trigger = $form_state
    ->getTriggeringElement();
  $field_name = $trigger['#rng_field_name'];
  $entity_type = $event_type
    ->getEventEntityTypeId();
  $bundle = $event_type
    ->getEventBundle();

  // Create the field.
  rng_add_event_field_storage($field_name, $entity_type);
  $field_config = rng_add_event_field_config($field_name, $entity_type, $bundle);
  \Drupal::messenger()
    ->addMessage(t('Field %field_name added.', [
    '%field_name' => $field_config
      ->label(),
  ]));

  // Make the field visible on the edit form.
  $display = \Drupal::service('entity_display.repository')
    ->getFormDisplay($entity_type, $bundle, 'rng_event');
  rng_add_event_form_display_defaults($display, $field_name);
  $component = $display
    ->getComponent($field_name);
  $component['weight'] = 9999;
  $display
    ->setComponent($field_name, $component);
  $display
    ->save();
}