Score:0

How do I set an item definition class for a configuration field?

us flag

I am not able to set an item definition class for a configuration field.

I tried implementing hook_entity_bundle_field_info_alter() , but it doesn't work.

function hook_entity_bundle_field_info_alter(&$fields, \Drupal\Core\Entity\EntityTypeInterface $entity_type, $bundle) {
  if ($entity_type->id() === 'my_entity_type_id') {
    foreach ($fields as $field) {
      if ($field->getType() === 'file') {
        $field->getItemDefinition()->setClass(AnswerFileItem::class);
      }
    }
  }
}

It seems Drupal is ignoring it and constructs item using the base class.

My file item class:

use Drupal\file\Plugin\Field\FieldType\FileItem;

class AnswerFileItem extends FileItem {

  /**
   * {@inheritDoc}
   */
  public function getUploadLocation($data = []) {
    $data += ['my_entity_type_id' => $this->getEntity()];
    return parent::getUploadLocation($data);
  }

}

Basically I want to add additional token data to all configuration fields on given entity_type_id.

Entity class is not important, it could be node.

I think problem is in FieldItemList because it uses wrong field item class.

Hermann Schwarz avatar
cn flag
Could you provide the original code you actually use. You can debug and find out where the control flow goes in and where not. Especially: is there any field in Entity my_entity_type_d? Is AnswerFileItem visible/imported?
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.