Update field values in multiple paragraphs

Let's say you have a LOT of paragraphs on your site. (Meaning Drupal Paragraphs contributed module entities).

Now you want to update a field in all of them. Rather than trying to find each and every one within nodes, you can run a simple script to update a field value.

This will simply load all paragraphs of a specific bundle. Then for each one, you update a value then save.

If you have a lot of paragraphs, you may want to run a batch process.

// Load all the paragraph entities of type
$entityManager = \Drupal::entityTypeManager();
$entities = $entityManager
    ->getStorage('paragraph')
    ->loadByProperties(['type' => 'MY_PARAGRAPH_TYPE','status' => 1]);

// Looping through each one and changing a value
// This could/should be set up in a batch process for a lot of items
foreach($entities as $key => $entity) {
    $paragraphStorage = $entityManager->getStorage('paragraph');
    $paragraph = $paragraphStorage->load($key);
    $paragraph->set('field_my_field','MY_NEW_VALUE');
    $paragraph->save();
}