You are here

function field_collection_field_storage_config_insert in Field collection 8

Same name and namespace in other branches
  1. 8.3 field_collection.module \field_collection_field_storage_config_insert()

Implements hook_ENTITY_TYPE_insert() for field_storage_config.

Create a field collection bundle when a new field collection field is made.

File

./field_collection.module, line 38
Module implementing field collection field type.

Code

function field_collection_field_storage_config_insert(FieldStorageConfigInterface $field) {
  if (Drupal::isConfigSyncing()) {
    return;
  }
  if ($field
    ->getType() == 'field_collection') {
    $field_collection_exists = \Drupal::entityQuery('field_collection')
      ->condition('id', $field
      ->getName())
      ->count()
      ->execute();
    if (!$field_collection_exists) {
      $values = [
        'label' => $field
          ->getName(),
        'id' => $field
          ->getName(),
      ];
      $field_collection = \Drupal::service('entity.manager')
        ->getStorage('field_collection')
        ->create($values);
      $field_collection
        ->enforceIsNew();
      $field_collection
        ->save();
    }

    // TODO: entity_invoke_bundle_hook in post save like in nodeType ?
    // Clear caches.

    //entity_info_cache_clear();

    // Do not directly issue menu rebuilds here to avoid potentially multiple
    // rebuilds. Instead, let menu_get_item() issue the rebuild on the next
    // request.
    //
    // TODO: Figure out whether this is still needed and replace it with the
    // new API if it is.
    // https://drupal.org/node/2183531
    //
    // variable_set('menu_rebuild_needed', TRUE);
  }
}