0

I want to create a text field to videos with code. I have the following in the .install file in a custom module:

function video_update_8101() {
  $field_name = 'field_video_alternative_text';
  // Make sure the field doesn't already exist.
  if (!FieldStorageConfig::loadByName('file', $field_name)) {
    // Create the field.
    $field = FieldStorageConfig::create(array(
      'field_name' => $field_name,
      'type' => 'text',
      'settings' => array('max_length' => 64),
    ));
    $field->save();

    // Create the instance.
    $instance = FieldStorageConfig::create(array(
      'field_name' => $field_name,
      'entity_type' => 'file',
      'bundle' => 'video',
      'label' => 'Alternative tekst',
      'description' => 'En beskrivelse kommer her.',
      'required' => TRUE,
    ));
    $instance->save();
  }
}

I get the following when running update.php

Attempt to create a field storage field_video_alternative_text with no entity_type.                                                                                                                                                                                 [error]
Performing emu_video_update_8101                                                                                                                                                                                                                                    [ok]
Failed: Attempt to create a field storage field_video_alternative_text with no entity_type.

Furthermore, I want this to happen when I install the module. I suppose it's not at this point?

Edit: I got inspiration from this post: How do programmatically create fields for content types, and ADD them to the the content type form

4
  • To the last point: hook_update_N is for updates only, they won't be executed upon install. The point of them is to allow older versions of the module to get up to date with the current version that they're upgrading to. The initial state of the latest version of the module should reflect the state the older version of the module would be in after the update. Think hook_install or config/install/*.yml
    – Clive
    Apr 4, 2019 at 13:51
  • So simply changing the function name to video_install will do that part? And in that case, will that then be the right way to do it rather than config/install?
    – NicklasF
    Apr 4, 2019 at 13:56
  • Yeah exactly. You might want to think about removing it again in hook_uninstall, or at least checking whether it exists first before creating in hook_install. Just in case you need to reinstall the module
    – Clive
    Apr 4, 2019 at 13:57
  • Ah, you edited that comment after I replied...the "yeah exactly" was to the first part. My own opinion is that adding config/install/field.storage.node.field_name.yml and the complementary field.field one is the right way to approach it
    – Clive
    Apr 4, 2019 at 13:58

1 Answer 1

1

In Drupal 7 fields were shared across all content types; in Drupal 8 they're shared across a single entity type.

As such you've just missed the entity_type when creating the storage:

$field = FieldStorageConfig::create(array(
  'entity_type' => 'file',
  'field_name' => $field_name,
  'type' => 'text',
  'settings' => array('max_length' => 64),
));

As an aside, hook_update_N is for updates only, those functions won't be executed upon install. The point of them is to allow older versions of the module to get up to date with the current version that they're upgrading to. The initial state of the latest version of the module should reflect the state the older version of the module would be in after the update.

You can use hook_install or config files in config/install/*.yml to perform initial setup of your module upon install.

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.