3

I generated with drupal console utility my own module and content entity code. After I added a user entity reference field and a taxonomy term entity reference field.

Now I'm listing my content with views, I don't do nothing with the code generated the integration is done.

But, when I expose a filter by taxonomy field and user field it doesn't give me the option to show as a dropdown, it directly appears as a textfield.

In others views, listing nodes with entity reference fields, if I add exposed filter of entity reference offers to me a dropdown or autocomplete widget.

What I have to change to my entity to have dropdown select for my entity reference field as exposed filter in a view?

Here is the field definition in the Entity class

    $fields['term_id'] = BaseFieldDefinition::create('entity_reference')
  ->setLabel(t('Log type'))
  ->setDescription(t('The term ID of the Log entry type.'))
  ->setRevisionable(FALSE)
  ->setSetting('target_type', 'taxonomy_term')
  ->setSetting('handler', 'default:taxonomy_term')
  ->setSetting('handler_settings', 
    array(
      'target_bundles' => array(
        'logs_type' => 'logs_type'
    )))
  ->setTranslatable(FALSE)
  ->setDisplayOptions('view', array(
    'label' => 'inline',
    'type' => 'taxonomy_term',
    'weight' => -2,
  ))
  ->setDisplayOptions('form', array(
    'type' => 'entity_reference_autocomplete',
    'weight' => -2,
    'settings' => array(
      'match_operator' => 'CONTAINS',
      'size' => '60',
      'autocomplete_type' => 'tags',
      'placeholder' => '',
    ),
  ))
  ->setDisplayConfigurable('form', TRUE)
  ->setDisplayConfigurable('view', TRUE);

Update:

I achieve to have a dropdown in the exposed filter in my view creating the file mymodule.views.inc and puting this code:

function log_activity_views_data_alter(&$data) {
  $data['log']['term_log_tid_depth'] = array(
    'help' => t('Display content if it has the selected taxonomy terms, or children of the selected terms. Due to additional complexity, this has fewer options than the versions without depth.'),
    'real field' => 'term_id',
    'argument' => array(
      'title' => t('Log type (with depth)'),
      'id' => 'taxonomy_index_tid_depth',
      'accept depth modifier' => TRUE,
    ),
    'filter' => array(
      'title' => t('Has taxonomy terms (with depth)'),
      'id' => 'taxonomy_index_tid_depth',
    ),
  );
}

But when I choose a term to filter the view, it does not return any item and it should do so.

4

As Berdir said, as long as https://www.drupal.org/node/2429699 is not ready, you need to provide your own Views Filter plugin.

I created this plugin, based on TaxonomyIndexTid, for Nodes, see https://gist.github.com/StryKaizer/ae1cb9abc4844a9e7ac12317a9d84a78

You need to place this file in a custom module at yourmodule/src/Plugin/views/filter/NodeIndexNid.php, and also implement hook_views_data_alter in your yourmodule.module file, like this:

/**
 * Implements hook_field_views_data_alter().
 *
 * Views integration for entity reference fields which reference nodes.
 * Adds a term relationship to the default field data.
 *
 * @see views_field_default_views_data()
 */
function yourmodule_field_views_data_alter(array &$data, FieldStorageConfigInterface $field_storage) {
  if ($field_storage->getType() == 'entity_reference' && $field_storage->getSetting('target_type') == 'node') {
    foreach ($data as $table_name => $table_data) {
      foreach ($table_data as $field_name => $field_data) {
        if (isset($field_data['filter']) && $field_name != 'delta') {
          $data[$table_name][$field_name]['filter']['id'] = 'node_index_nid';
        }
      }
    }
  }
}
1

Views currently only offers that for taxonomy_term references, as taxonomy.module provides a special Views integration.

There are core issues open to make this generic, for example: https://www.drupal.org/node/2429699.

Until that is resolved, you have to provide your own Views Filter plugin for this, based on \Drupal\taxonomy\Plugin\views\filter\TaxonomyIndexTid, and add a views_data handler to your entity type where you override the used filter for that field, like taxonomy.module does for all entity reference fields pointing to terms in taxonomy_field_views_data_alter()

  • I added hook_field_views_data_alter() to my custom entity module and I added \Drupal\my_custom_entity\Plugin\views\filter\TaxonomyIndexTid. But I doesn't work, I don't know what I have to change to make it work. – ferriol Jan 30 '17 at 16:04
  • That hook is for field-type providing modules, for a custom entity type it belongs in your EntityViewsData handler. Also, you'll have to share your code or we won't be able to help more. – Berdir Jan 30 '17 at 16:07
  • I solved adding a relation with taxonomy, and then in the filter section appear the filter of Term as dropdown or autocomplete. I think this question can help me with a filter for a date field, but my solution doesn't work for a date field :( – ferriol Jan 30 '17 at 23:16
1

The module Entity Reference Exposed Filters offers the possibility to generate a Views exposed filter of the referenced Titles entities.

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.