0

This is the code I am attempting to use to update a content type with a new field. When I run drush dbup I get this error.

PHP Fatal error: Using $this when not in object context in EntityStoageBase.php.

function some_config_update_8101() {
  if (!FieldConfig::load('image_byline')) {
    $config_path = drupal_get_path('module', 'some_config') . '/config/install/field.field.node.main_image.field_image_byline.yml';
    $data = Yaml::parse($config_path);
    \Drupal::configFactory()->getEditable('field.field.node.main_image.field_image_byline')->setData($data)->save(TRUE);
    $message .= "FieldConfig installed for module some_config.\n";
  }
  else {
    $message .= "It didn't work!\n";
  }

  if (!FieldConfigStorage::load('image_byline')) {
    $config_path = drupal_get_path('module', 'some_config') . '/config/install/field.storage.node.field_image_byline.yml';
    $data = Yaml::parse($config_path);
    \Drupal::configFactory()->getEditable('field.storage.node.field_image_byline')->setData($data)->save(TRUE);
    $message .= "FieldConfigStorage installed for module some_config.\n";
  }
  else {
    $message .= "That didn't work either!\n";
  }
}

How is my code affecting the context of EntityStorageBase, if I have not changed code in that file?

0

2 Answers 2

1

Updating Entities and Fields in Drupal 8, in particular Updating field storage config items, shows the following example code.

if (!$field_storage_configs = \Drupal::entityTypeManager()->getStorage('field_storage_config')->loadByProperties(array('type' => 'taxonomy_term_reference'))) {
  return;
}

foreach ($field_storage_configs as $field_storage) {
  // Since the usual workflow for field storages do not allow changing the
  // field type, we have to work around it in this case.
  $new_field_storage = $field_storage->toArray();
  $new_field_storage['type'] = 'entity_reference';
  $new_field_storage['module'] = 'core';
  $new_field_storage['settings']['target_type'] = 'taxonomy_term';

  $vocabulary_name = $new_field_storage['settings']['allowed_values'][0]['vocabulary'];
  unset($new_field_storage['settings']['allowed_values']);

  $new_field_storage = FieldStorageConfig::create($new_field_storage);
  $new_field_storage->original = $new_field_storage;
  $new_field_storage->enforceIsNew(FALSE);

  $new_field_storage->save();
}

That example code updates an entity field to change its type; it doesn't add an entity field if that doesn't exist. Still, comparing that code with the code you are using, it's clear you are using the wrong class: It's FieldStorageConfig, which is a a configuration entity class, not FieldConfigStorage, which is a storage handler class.

Being a class that implements an entity, FieldStorageConfig has the create() static method and the load() static method.

The example code is still useful for your case, since it shows how to create a new entity field storage configuration entity. In your case, you will check first the field storage configuration entity doesn't already exist by calling FieldStorageConfig::load().

0

FieldConfigStorage::load() isn't static, so you can't use it statically.

Get an instance of FieldConfigStorage instead, and use that

if ($storage->load('image_byline')) {
  // ...
}
3
  • I have used Node::load() why is this any different? dont they both override EntityStorageInterface::load ? Mar 17, 2016 at 16:17
  • 1
    No, one's for the config (extends FieldConfigBase) and the other is for storage (FieldConfigStorageBase). Totally different classes, though they complement one another
    – Clive
    Mar 17, 2016 at 16:21
  • 1
    The example just uses the wrong class name, should be FieldStorageConfig, wihch is a config entity too. Also, the ID of them is entity_type.bundle.fieldname for the field config, and entity_type.fieldname for the field storage config.
    – Berdir
    Mar 17, 2016 at 16:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.