protected function createFieldInstances($message_type, array $fields) {
    foreach ($fields as $field) {
      $id = 'message.' . $message_type . '.' . $field['name'];
      $config_storage = \Drupal::entityTypeManager()
        ->getStorage('field_config');
      // Create field instances if they do not exists.
      if ($config_storage->load($id) === NULL) {
        $field_instance = [
          'langcode' => 'en',
          'status' => TRUE,
          'config' => [
            'field.storage.message.' . $field['name'],
            'message.template.' . $message_type,
          ],
          'module' => ['options'],
          'id' => $id,
          'field_name' => $field['name'],
          'entity_type' => 'message',
          'bundle' => $message_type,
          'label' => '',
          'description' => '',
          'required' => FALSE,
          'translatable' => FALSE,
          'default_value' => [],
          'default_value_callback' => '',
          'field_type' => $field['type'],
        ];

        if ($field['type'] === 'list_string') {
          $field_instance['module'] = ['options'];
          $field_instance['settings'] = [];
        }
        elseif ($field['type'] === 'dynamic_entity_reference') {
          $field_instance['module'] = ['dynamic_entity_reference'];
          $field_instance['settings'] = [];
        }
        $config_storage->create($field_instance)->save();
      }
    }
  }
protected function createFieldInstances($message_type, array $fields) {
    foreach ($fields as $field) {
      $id = 'message.' . $message_type . '.' . $field['name'];
      $config_storage = \Drupal::entityTypeManager()
        ->getStorage('field_config');
      // Create field instances if they do not exists.
      if ($config_storage->load($id) === NULL) {
        $field_instance = [
          'langcode' => 'en',
          'status' => TRUE,
          'config' => [
            'field.storage.message.' . $field['name'],
            'message.template.' . $message_type,
          ],
          'module' => ['options'],
          'id' => $id,
          'field_name' => $field['name'],
          'entity_type' => 'message',
          'bundle' => $message_type,
          'label' => '',
          'description' => '',
          'required' => FALSE,
          'translatable' => FALSE,
          'default_value' => [],
          'default_value_callback' => '',
          'field_type' => $field['type'],
        ];

        if ($field['type'] === 'list_string') {
          $field_instance['module'] = ['options'];
          $field_instance['settings'] = [];
        }
        elseif ($field['type'] === 'dynamic_entity_reference') {
          $field_instance['module'] = ['dynamic_entity_reference'];
          $field_instance['settings'] = [];
        }
        $config_storage->create($field_instance)->save();
      }
    }
  }
protected function createFieldInstances ( $ message_type , array $ fields ) { foreach ( $ fields as $ field ) { $ id = 'message.' . $ message_type . '.' . $ field [ 'name' ] ; $ config_storage = \ Drupal :: entityTypeManager ( ) -> getStorage ( 'field_config' ) ; // Create field instances if they do not exists. if ( $ config_storage -> load ( $ id ) === NULL ) { $ field_instance = [ 'langcode' => 'en' , 'status' => TRUE , 'config' => [ 'field.storage.message.' . $ field [ 'name' ] , 'message.template.' . $ message_type , ] , 'module' => [ 'options' ] , 'id' => $ id , 'field_name' => $ field [ 'name' ] , 'entity_type' => 'message' , 'bundle' => $ message_type , 'label' => '' , 'description' => '' , 'required' => FALSE , 'translatable' => FALSE , 'default_value' => [ ] , 'default_value_callback' => '' , 'field_type' => $ field [ 'type' ] , ] ; if ( $ field [ 'type' ] === 'list_string' ) { $ field_instance [ 'module' ] = [ 'options' ] ; $ field_instance [ 'settings' ] = [ ] ; } elseif ( $ field [ 'type' ] === 'dynamic_entity_reference' ) { $ field_instance [ 'module' ] = [ 'dynamic_entity_reference' ] ; $ field_instance [ 'settings' ] = [ ] ; } $ config_storage -> create ( $ field_instance ) -> save ( ) ; } } }
Create field instances.

@param string $message_type
The typeof message.
@param array $fields
The data to insert in the field instances.
Create field instances .