1

I develop a custom statistics module to log all node views of a chosen bundles, file downloads and the keywords used to filter a view.

Then I build my Entity Log with a user reference (who does the action), a node reference and a file reference. I want to know if I can make a views reference to point the View (as a configuration entity) that the user searched for some keyword.

I don't know if this is crazy... Is it possible to reference a view from an entity?

I'm using drupal 8

closed as unclear what you're asking by Kevin, Pierre.Vriens, mradcliffe, Adrian Cid Almaguer, kenorb Jan 18 '17 at 16:06

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Open-ended questions like "is this possible?" are not helpful for answers. The answer is Yes, this is possible. The message module already does something similar (stores messages that are fieldable with entity references). However I think that if you are logging for reporting, then the best option for scalability (large number of records) is to use a denormalized/reporting data model rather than a transactional data model. – mradcliffe Jan 18 '17 at 13:55
  • Sorry, I edited the question to be more clear. – ferriol Jan 18 '17 at 17:06
  • @mradcliffe I interested in how can I implement this "use a denormalized/reporting data model" – ferriol Jan 18 '17 at 17:07
2

This is no problem, in ui you can configure bundle fields referencing all entity types, content and configuration. And for base fields this is an example from the user entity for a reference field pointing at the configuration entity user_role:

$fields['roles'] = BaseFieldDefinition::create('entity_reference')
  ->setLabel(t('Roles'))
  ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
  ->setDescription(t('The roles the user has.'))
  ->setSetting('target_type', 'user_role');
  • I found it, also it can reference a view $fields['view_id'] = BaseFieldDefinition::create('entity_reference') ->setLabel(t('View')) ->setDescription(t('The view ID of the Log entry.')) ->setSetting('target_type', 'view') ->setDisplayOptions('view', array( 'label' => 'hidden', 'type' => 'entity_reference_label', 'weight' => 1, )); – ferriol Jan 18 '17 at 17:04

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