Answers.Body: <p>After I dig a little bit deeper into views and view some other examples - now it works:</p>
  <pre><code>if (!empty($this-&gt;value)) {
    $this-&gt;ensureMyTable(); // to get the tableAlias
    $configuration = [
      'table' =&gt; 'civicrm_entity_tag',
      'field' =&gt; 'entity_id',
      'left_table' =&gt; $this-&gt;tableAlias, // set the table alias
      'left_field' =&gt; 'id',
      'operator' =&gt; '=',
    ];
    $join = Views::pluginManager('join')-&gt;createInstance('standard', $configuration);
    $this-&gt;query-&gt;addRelationship('civicrm_entity_tag', $join, 'civicrm_contact');
    $this-&gt;query-&gt;addWhere('AND', 'civicrm_entity_tag.tag_id', $this-&gt;value, 'IN');
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-03-07T17:56:53.317Z
Answers.LastActivityDate: 2024-03-07T17:56:53.317Z
Answers.UserDisplayName: ItsMe
Body: <p>Need a hint. With civicrm_entity (3.5) i haven't the possibility to get multiple items in a views row. So i wrote a custom field and now I want to filter it.</p>
  <p>I think this works for the contact type (Organization), but I need to filter the related contacts (Individual) by the attached tags.</p>
  <pre><code>if (!empty($this-&gt;value)) {
    $configuration = [
      'table' =&gt; 'civicrm_entity_tag',
      'field' =&gt; 'entity_id',
      'left_table' =&gt; 'civicrm_contact', 
      'left_field' =&gt; 'id', 
      'operator' =&gt; '=',
    ];
    $join = Views::pluginManager('join')-&gt;createInstance('standard', $configuration);
    $this-&gt;query-&gt;addRelationship('civicrm_entity_tag', $join, 'civicrm_contact');
    $this-&gt;query-&gt;addWhere('AND', 'civicrm_entity_tag.tag_id', $this-&gt;value, 'IN');
  
    dump($query-&gt;options['relationship']) // i get 'contact_id_a' thats correct with the relationship, but how can i use it?
  
  }
  </code></pre>
  
Tags: <drupal-views><civicrm-entity>
Title: Custom views filter with civicrm_entity Drupal 9
ThreadText: # Custom views filter with civicrm_entity Drupal 9
  
  (Asked by: username_0 on 2024-03-06)
  
  Need a hint. With civicrm_entity (3.5) i haven't the possibility to get multiple items in a views row. So i wrote a custom field and now I want to filter it.
  I think this works for the contact type (Organization), but I need to filter the related contacts (Individual) by the attached tags.
  if (!empty($this->value)) {
    $configuration = [
      'table' => 'civicrm_entity_tag',
      'field' => 'entity_id',
      'left_table' => 'civicrm_contact', 
      'left_field' => 'id', 
      'operator' => '=',
    ];
    $join = Views::pluginManager('join')->createInstance('standard', $configuration);
    $this->query->addRelationship('civicrm_entity_tag', $join, 'civicrm_contact');
    $this->query->addWhere('AND', 'civicrm_entity_tag.tag_id', $this->value, 'IN');
  
    dump($query->options['relationship']) // i get 'contact_id_a' thats correct with the relationship, but how can i use it?
  
  }
  
  **Tags:** drupal-views, civicrm-entity
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2024-03-07. Score: 1
  
  After I dig a little bit deeper into views and view some other examples - now it works:
  if (!empty($this->value)) {
    $this->ensureMyTable(); // to get the tableAlias
    $configuration = [
      'table' => 'civicrm_entity_tag',
      'field' => 'entity_id',
      'left_table' => $this->tableAlias, // set the table alias
      'left_field' => 'id',
      'operator' => '=',
    ];
    $join = Views::pluginManager('join')->createInstance('standard', $configuration);
    $this->query->addRelationship('civicrm_entity_tag', $join, 'civicrm_contact');
    $this->query->addWhere('AND', 'civicrm_entity_tag.tag_id', $this->value, 'IN');
  }
  
  
  ---
  
  