1

I want to create custom content entity, with entity reference field, and display it by views.

Reference field is defined by:

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

How I'm supposed to use this field in view, to display referenced term names ?

4
  • I think you can either display the field using its display mode or add the taxonomy relationship in your view in Views. Is that what you're asking?
    – mradcliffe
    Mar 25, 2016 at 11:59
  • Problem is that I can display numeric ID from this field in views. But to display term's name based on ID I'd need to add taxonomy table under Views Relationship. But there are no table at all under Views Relationships. s28.postimg.org/lxamxxoy5/rel1.jpg
    – laniol
    Mar 25, 2016 at 12:04
  • 1
    This is a missing feature in core, being worked on in drupal.org/node/2477899. For now, you have to define the relationships and views definitions yourself.
    – Berdir
    Mar 25, 2016 at 13:55
  • Could you hint me how to add these relationships? maybe some link
    – laniol
    Mar 29, 2016 at 7:59

2 Answers 2

1

The easy solution is to create the field via the interface instead of creating it as a base field. Drupal core still have some issues with basefields not working exactly the same way as config fields (fields created as config via interface). Views is missing various integration, the same goes with date fields.

In your case, where you have a multi valued field, it would be store the same way, the only difference is the table name (ENTITY__FIELDNAME vs FIELDPREFIX_FIELDNAME).

If you really want to use base fields, you can make up from the missing integration by handling it yourself in a custom EntityViewsData class for your entity. Having done this in the past, my advice would be to just create a config field, since that is working pretty well, and doesn't change anything on the way the field is store in the database anyways.

4
  • In fact there is difference: single value field addeb via UI creates a new table in db, whereas the same field defined via BaseFieldDefinition adds only column in main entity table.
    – laniol
    Mar 29, 2016 at 8:19
  • @laniol Your field is not single valued! Multi valued fields are created in a separate table both as base fields and config fields, which is my point. There aren't any difference. If the field was single valued you wouldn't be having any issue.
    – googletorp
    Mar 29, 2016 at 8:30
  • yes, I know. I just have both single and multi-value fields in this custom entity.. What could be working for now is to store single-value field definitions in BaseFieldDefinition and store multi-value fields in .yml files ... which is kind of odd
    – laniol
    Mar 29, 2016 at 8:37
  • @laniol Like I wrote in my answer, you can also handle the missing parts yourself. You can pretty much copy/paste what core does for config fields. This is not something I would recommend as it makes is harder to maintain this solution.
    – googletorp
    Mar 29, 2016 at 11:49
0

As of Drupal 8.4.2 this still an issue.

Here is how I fixed it having a custom Entity Content "itrack_notification" and an entity reference field "entity_reference" created with BaseFieldDefinition to be able to use this field as a contextual filter.

In my_module/src/Entity/myEntityViewsData.php

/**
 * {@inheritdoc}
 */
public function getViewsData() {
  $data = parent::getViewsData();

  $reference_field_name = 'receivers';
  $entity_type = 'itrack_notification';

  $handler_values = [
    'field' => "{$reference_field_name}_target_id",
    'table' => "{$entity_type}__{$reference_field_name}",
    'field_name' => $reference_field_name,
    'entity_type' => $entity_type,
    'additional fields' => [
      "field_{$reference_field_name}_target_id",
    ],
  ];
  $data["{$entity_type}__{$reference_field_name}"][$reference_field_name]['relationship']['relationship field'] = "{$reference_field_name}_target_id";
  $data["{$entity_type}__{$reference_field_name}"]["{$reference_field_name}_target_id"] = [
    'group' => $this->t('ITrack Notification'),
    'argument' => [
        'title' => $this->t('Receivers - has User ID'),
        'help' => $this->t('Holds the receiver User ID.'),
        'id' => 'numeric',
      ] + $handler_values,
  ];
  // Unset unusable view data handlers.
  unset($data["{$entity_type}__{$reference_field_name}"][$reference_field_name]['argument']);
  return $data;
}

If you need the fix for using the field as a filter or sort have a look at this patch.

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.