4

I've created custom content entity, where one of fields defined in BaseFieldDefinitions() is

$fields['game_category'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Game category'))
    ->setDescription(t('Game category'))
    ->setSetting('target_type', 'taxonomy_term')
    ->setRevisionable(false)
    ->setCardinality(-1);
;

Now, I'm trying to create a list using Views, where one of displayed fields will be "Game category" term name. What I managed to achieve is to display that term's tid.

QUESTION: How to display term's name ?

I suppose I should add a relationship, so Views is able to make a JOIN with taxonomy table

My class exposing fields to Views looks like this:

class GameViewsData extends EntityViewsData implements EntityViewsDataInterface  {

public function getViewsTableForEntityType(EntityTypeInterface $entity_type) {
    return $entity_type->getDataTable() ? : $entity_type->getBaseTable();
}

public function getViewsData() {

$data = parent::getViewsData();

$data['game'] = array();
$data['game']['table']['group'] = t('Game Entity Table');
$data['game']['table']['provider'] = 'game_entity';

$data['game']['table']['base'] = array(
    'field' => 'id',
    'title' => t('Game Entity Table'),
    'help' => t('Game Entity Table'),
    'weight' => -10,
);

$data['game']['name'] = array(
    'title' => t('name'),
    'help' => t('name'),
    'field' => array(
        'id' => 'standard',
    ),
    'sort' => array(
        'id' => 'standard',
    ),
    'filter' => array(
        'id' => 'string',
    ),
    'argument' => array(
        'id' => 'string',
    ),
);

$data['game_entity_game__game_category'] = array();
$data['game_entity_game__game_category']['table']['group'] = t('Game Entity Table');
$data['game_entity_game__game_category']['table']['provider'] = 'game_entity';

$data['game_entity_game__game_category']['table']['join'] = array(
    'game' => array(
        'left_field' => 'id',
        'field' => 'entity_id',
    ),
);

$data['game_entity_game__game_category']['game_category_target_id'] = array(
    'title' => t('game_category_target_id'),
    'help' => t('game_category_target_id'),
    'field' => array(
        'id' => 'standard',
    ),
    'sort' => array(
        'id' => 'standard',
    ),
    'filter' => array(
        'id' => 'string',
    ),
    'argument' => array(
        'id' => 'string',
    ),
);

return $data;
}

}

1

You didn't say that you wanted to do this programmatically, so I'll give you the answer for doing it in the UI. Since you have defined the field game_category as an entity_reference, you can add a relationship in your view to Taxonomy. See Add a relationship to a view for an example.

Then, any field in Taxonomy will be available in your view, including the name. Since this is built into the Views UI by default, you do not need any of your getViewsData() custom code.

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.