You are here

public function RngEventType::postSave in RNG - Events and Registrations 8.2

Same name and namespace in other branches
  1. 3.x src/Entity/RngEventType.php \Drupal\rng\Entity\RngEventType::postSave()

Acts on a saved entity before the insert or update hook is invoked.

Used after the entity is saved, but before invoking the insert or update hook. Note that in case of translatable content entities this callback is only fired on their current translation. It is up to the developer to iterate over all translations if needed.

Parameters

\Drupal\Core\Entity\EntityStorageInterface $storage: The entity storage object.

bool $update: TRUE if the entity has been updated, or FALSE if it has been inserted.

Overrides EntityBase::postSave

File

src/Entity/RngEventType.php, line 408

Class

RngEventType
Defines the event type entity.

Namespace

Drupal\rng\Entity

Code

public function postSave(EntityStorageInterface $storage, $update = TRUE) {
  parent::postSave($storage, $update);

  // Create mode for the entity type.
  $mode_id = $this->entity_type . '.rng_event';
  if (!EntityFormMode::load($mode_id)) {
    EntityFormMode::create([
      'id' => $mode_id,
      'targetEntityType' => $this->entity_type,
      'label' => 'Event Settings',
      'status' => TRUE,
    ])
      ->save();
  }
  if (!$update) {
    module_load_include('inc', 'rng', 'rng.field.defaults');
    foreach ($this->fields as $field) {
      rng_add_event_field_storage($field, $this->entity_type);
      rng_add_event_field_config($field, $this
        ->getEventEntityTypeId(), $this
        ->getEventBundle());
    }
  }
  $display = entity_get_form_display($this->entity_type, $this->bundle, 'rng_event');
  if ($display
    ->isNew()) {

    // EntityDisplayBase::init() adds default fields. Remove them.
    foreach (array_keys($display
      ->getComponents()) as $name) {
      if (!in_array($name, $this->fields)) {
        $display
          ->removeComponent($name);
      }
    }

    // Weight is the key.
    $field_weights = [
      EventManagerInterface::FIELD_STATUS,
      EventManagerInterface::FIELD_ALLOW_DUPLICATE_REGISTRANTS,
      EventManagerInterface::FIELD_WAIT_LIST,
      EventManagerInterface::FIELD_REGISTRANTS_CAPACITY,
      EventManagerInterface::FIELD_EMAIL_REPLY_TO,
      EventManagerInterface::FIELD_REGISTRATION_TYPE,
      EventManagerInterface::FIELD_REGISTRATION_GROUPS,
    ];
    module_load_include('inc', 'rng', 'rng.field.defaults');
    foreach ($this->fields as $name) {
      rng_add_event_form_display_defaults($display, $name);
      if (in_array($name, $field_weights)) {
        $component = $display
          ->getComponent($name);
        $component['weight'] = array_search($name, $field_weights);
        $display
          ->setComponent($name, $component);
      }
    }
    $display
      ->save();
  }

  // Rebuild routes and local tasks.
  \Drupal::service('router.builder')
    ->setRebuildNeeded();

  // Rebuild local actions https://github.com/dpi/rng/issues/18
  \Drupal::service('plugin.manager.menu.local_action')
    ->clearCachedDefinitions();
}