Answers.Body: <p>I found a way:</p>
  <p>This function checks recursively whether a field exists with the searched type, if it is a matrix field then it searches further in it.</p>
  <pre class="lang-php prettyprint-override"><code>
  private function filterFieldsByType($blocks, $fieldType): array
  {
      $values = [];
  
      foreach ($blocks as $block) {
          foreach ($block-&gt;getFieldLayout()-&gt;getFields() as $field) {
              if ($field instanceof $fieldType) {
                  $values[] = $block-&gt;{$field-&gt;handle};
              }
              elseif ($field instanceof \craft\fields\Matrix) {
                  $values[] = $this-&gt;filterFieldsByType($block-&gt;{$field-&gt;handle}, $fieldType);
              }
          }
      }
      return $values;
  }
  
  </code></pre>
  <p>This is the main function that returns all fields in which the searched field type exists, the whole is returned as an array where the fields with the values are located.</p>
  <pre class="lang-php prettyprint-override"><code>
  public function getFieldsFromEntriesByFieldType($fieldType): array
  {
      $entries = Entry::find()-&gt;all(); //Maybe batch query for better perfomance if there is a large of amount.
      $values = $this-&gt;filterFieldsByType($entries, $fieldType);
  
      $result = array_merge([], ...$values);
      $result = array_filter($result);
      return array_values($result);
  }
  
  </code></pre>
  <p>How to use it:</p>
  <pre class="lang-php prettyprint-override"><code>$fieldType = 'xxx\\myplugin\\fields\\MyCustomField';
  
  $myCustomFieldsFromAllEntries = $this-&gt;getFieldsFromEntriesByFieldType($fieldType);
  
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-02-09T12:33:52.163Z
Answers.LastActivityDate: 2024-02-09T12:33:52.163Z
Answers.UserDisplayName: antdev
Body: <p>I am trying to get all values of a specific field type from all entries. (ex.: craft\fields\PlainText)</p>
  <p>Is there an easy way to get these values ?</p>
  
Tags: <craft3><plugin-development><field><entries-field><custom-fieldtype>
Title: How to get field values by specific field type from all Entries?
ThreadText: # How to get field values by specific field type from all Entries?
  
  (Asked by: username_0 on 2024-02-09)
  
  I am trying to get all values of a specific field type from all entries. (ex.: craft\fields\PlainText)
  
  Is there an easy way to get these values ?
  
  **Tags:** craft3, plugin-development, field, entries-field, custom-fieldtype
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2024-02-09. Score: 1 | Accepted answer
  
  I found a way:
  
  This function checks recursively whether a field exists with the searched type, if it is a matrix field then it searches further in it.
  
  ```php
  
  private function filterFieldsByType($blocks, $fieldType): array
  {
      $values = [];
  
      foreach ($blocks as $block) {
          foreach ($block->getFieldLayout()->getFields() as $field) {
              if ($field instanceof $fieldType) {
                  $values[] = $block->{$field->handle};
              }
              elseif ($field instanceof \craft\fields\Matrix) {
                  $values[] = $this->filterFieldsByType($block->{$field->handle}, $fieldType);
              }
          }
      }
      return $values;
  }
  
  ```
  
  This is the main function that returns all fields in which the searched field type exists, the whole is returned as an array where the fields with the values are located.
  
  ```php
  
  public function getFieldsFromEntriesByFieldType($fieldType): array
  {
      $entries = Entry::find()->all(); //Maybe batch query for better perfomance if there is a large of amount.
      $values = $this->filterFieldsByType($entries, $fieldType);
  
      $result = array_merge([], ...$values);
      $result = array_filter($result);
      return array_values($result);
  }
  
  ```
  
  How to use it:
  
  ```php
  $fieldType = 'xxx\\myplugin\\fields\\MyCustomField';
  
  $myCustomFieldsFromAllEntries = $this->getFieldsFromEntriesByFieldType($fieldType);
  
  ```
  
  
  ---
  
  