3

consider the following situation: I have created a custom content entity with several base fields (string and string_long fields) and it works flawlessly. In addition I want the entity to hold an image field which I defined the same way I did with the other fields:

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {

    …

    $form_options = array(
        'type' => 'string',
        'weight' => $key,
    );

    $view_options = array(
        'label' => 'hidden',
        'type' => 'string',
        'weight' => $key,
    );

    …

    $type = 'image';
    $settings = array();
    $form_options['type'] = 'image_image';
    $view_options['type'] = 'image_image';

    …

    $fields[$fieldname] = BaseFieldDefinition::create($type)
        ->setLabel(t($sections['labels'][$key]))
        ->setDescription(t($sections['descriptions'][$key]))
        ->setSettings($settings)
        ->setDisplayOptions('view', $view_options)
        ->setDisplayOptions('form', $form_options);
}

Now, why would I get the errors Notice: Undefined index: target_type in Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem::propertyDefinitions() (line 70 of core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php). and Drupal\Component\Plugin\Exception\PluginNotFoundException: The "" entity type does not exist. in Drupal\Core\Entity\EntityManager->getDefinition() (line 256 of core/lib/Drupal/Core/Entity/EntityManager.php).?

Anyone has a hint? Thank you in advance!

4

Looks like you need to specify the target_type.

It should get the default from FileItem::defaultStorageSettings(), I am not sure why that doesn't work.

Unrelated, but note that you should not call t() like that, as it makes it impossible for potx to extract translatable strings from your code. Also, watch out for https://www.drupal.org/node/2363099, you need to update it to use nt() then.

Edit: Turns out, the problem is that the code was using setSettings(), which is considered problematic because it replaces all existing settings, including defaults. Switching to setSetting() for specific settings should solve it.

  • Well, using ->setSettings('target_type' => 'file') now gets me to the entity edit form where I get a lot more errors resulting from defaultFieldSettings not being applied. I see errors like Notice: Undefined index: file_directory in Drupal\file\Plugin\Field\FieldType\FileItem->getUploadLocation() (line 269 of core/modules/file/src/Plugin/Field/FieldType/FileItem.php). or Notice: Undefined index: max_resolution in Drupal\image\Plugin\Field\FieldWidget\ImageWidget->formElement() (line 120 of core/modules/image/src/Plugin/Field/FieldWidget/ImageWidget.php).… Maybe it is a bug? – alexej_d Jan 15 '15 at 23:30
  • 1
    Ah, you are using setSettings(). Don't. That replaces all existing (default) settings. Use setSetting($key, value) for your own settings, always. – Berdir Jan 16 '15 at 8:25
  • Hey, thanks, I did not know that! Now everything works fine! Care to update you answer? I will gladly accept it then. – alexej_d Jan 16 '15 at 9:03
  • Updated the answer. – Berdir Jan 16 '15 at 14:09
-1

You are using wrong formatter type, these is no "image_image" formatter so

$view_options['type'] = 'image_image'; 

should be

$view_options['type'] = 'image';

for more on how to get proper formatters ids look here https://www.drupaldump.com/custom-entities-and-how-get-setdisplayoptions-types

  • This is a very old issue which was already resolved. By then "image_image" worked… – alexej_d Sep 14 '18 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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