You are here

public static function BulkUpdateFields::updateFields in Bulk Update Fields 8

Same name and namespace in other branches
  1. 8.2 src/BulkUpdateFields.php \Drupal\bulk_update_fields\BulkUpdateFields::updateFields()

File

src/BulkUpdateFields.php, line 13

Class

BulkUpdateFields
BulkUpdateFields.

Namespace

Drupal\bulk_update_fields

Code

public static function updateFields($entities, $fields, &$context) {
  $message = 'Updating Fields...';
  $results_entities = [];
  $results_fields = [];
  $update = FALSE;
  foreach ($entities as $entity) {
    foreach ($fields as $field_name => $field_value) {
      if ($entity
        ->hasField($field_name)) {
        if ($field_value == $field_name) {
          continue;
        }

        // this is the case for field images for some reason
        // not sure if this is still valid but leaving in case
        if (isset($field_value['target_id'][0])) {
          $field_value = $field_value['target_id'];
        }

        // this caused a failure in core/entity/plugin/datatype/entityreference. removing.
        if (isset($field_value[0]['target_id']) && isset($field_value['add_more'])) {
          unset($field_value['add_more']);
        }

        // this occurs in fields like office hours.
        if (isset($field_value['value'])) {
          $field_value = $field_value['value'];
        }
        $entity
          ->get($field_name)
          ->setValue($field_value);
        $update = TRUE;
        if (!in_array($field_name, $results_fields)) {
          $results_fields[] = $field_name;
        }
      }
    }
    if ($update) {
      $entity
        ->setNewRevision();
      $entity
        ->save();
      $results_entities[] = $entity
        ->id();
    }
  }
  $context['message'] = $message;
  $context['results']['results_entities'] = $results_entities;
  $context['results']['results_fields'] = $results_fields;
}