Alter Multiple Field Values by Testing "Field Type" Drupal 8.x

I would like to alter the value of all/any ckeditor/text fields on a node.

Currently working with HOOK_node_presave().

Ideally, I'd like an agnostic approach that doesn't mind where the field is coming from, e.g. paragraph, node, taxonomy, etc.

How can I loop through all fields, check the field type, get and replace the value?



MYMODULE.module

Working with the below code gets me close I think:

use Drupal\Core\Entity\EntityInterface;

function MYMODULE_node_presave(EntityInterface $entity) {
  $fieldsArray = \Drupal::service('entity_field.manager')-&gt;getFieldMapByFieldType('text_long');

  foreach($fieldsArray as $longTextName=&gt;$key) {

    kpr($key); // This returns an array of the values I need.
    if ($entity-&gt;hasField($key)) {
      $entity-&gt;get($result)-&gt;value = preg_replace(
        $toReplace,
        $replaceWith,
        $entity-&gt;get($result)-&gt;value
      );
    }
  }

}




Try #2;

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;

function MYMODULE_presave(EntityInterface $entity) {
  // Get form config.
  // Get config Node field names.
  // Get config Paragraph names.
  // Get config Paraghraph field names.

  // Loop through our Node field names.
  foreach($nodeFieldNames as $fieldName) {
    // Check if the entity has field.
    if ($entity-&gt;hasField($fieldName)) {
      // Get the field.
      $entity-&gt;get($fieldName)-&gt;value = $newValue;
    }
  }

  // Loop through our Paragraph names.
  foreach($paragraphNames as $paragraphName) {
  // Check if the entity has field.
  if ($entity-&gt;hasField($paragraphName)) {
    $paragraphs = $entity-&gt;$paragraphName-&gt;getValue();
    foreach ($paragraphs as $paragraphItem) {
      $p = \Drupal\paragraphs\Entity\Paragraph::load( $paragraphItem['target_id'] );
      foreach($paragraphFieldNames as $paragraphFieldNamesitem) {
        if ($p-&gt;get($paragraphFieldNamesitem)-&gt;value) {
          // kpr($p-&gt;get($paragraphFieldNamesitem)-&gt;value);
          $p-&gt;get($paragraphFieldNamesitem)-&gt;value = $newvalue;
          // kpr($p-&gt;get($paragraphFieldNamesitem)-&gt;value);
          // This does alter the value, just does not get saved.
        }
      }
    }
  }
}
You can use FieldConfigInterface to get fields info.
Try the following:

use Drupal\Core\Field\FieldConfigInterface;

function MYMODULE_node_presave($node) {
  //- Call helper function to get all the fields
  $fieldsArray = getContentTypeFields($node-&gt;bundle());
  //- Loop through fields 
  foreach ($fieldsArray as $fieldName =&gt; $fieldConfig) {
    ///- Example to check if type is text_with_summary
    if ($fieldConfig-&gt;getType() == 'text_with_summary') {
      /**
       * @TODO : Something here for field of type text_with_summary
       */
    }
  }
}

/**
 * Helper function to Get all fields of content type:
 *
 * @param $contentType
 *
 * @return array
 */
function getContentTypeFields($contentType) {
  $entityManager = Drupal::service('entity.manager');
  $fields = [];
  if (!empty($contentType)) {
    $fields = array_filter(
      $entityManager-&gt;getFieldDefinitions('node', $contentType), function ($field_definition) {
      return $field_definition instanceof FieldConfigInterface;
    }
    );
  }

  return $fields;
}