5 * Post update functions for Paragraphs.
8 use Drupal\Core\Site\Settings;
9 use Drupal\field\Entity\FieldStorageConfig;
12 * Set the parent id, type and field name to the already created paragraphs.
16 function paragraphs_post_update_set_paragraphs_parent_fields(&$sandbox) {
17 // Don't execute the function if paragraphs_update_8003() was already executed
18 // which used to do the same.
20 $module_schema = drupal_get_installed_schema_version('paragraphs');
22 // The state entry 'paragraphs_update_8003_placeholder' is used in order to
23 // indicate that the placeholder paragraphs_update_8003() function has been
24 // executed, so this function needs to be executed as well. If the non
25 // placeholder version of paragraphs_update_8003() got executed already, the
26 // state won't be set and we skip this update.
27 if ($module_schema >= 8003 && !\Drupal::state()->get('paragraphs_update_8003_placeholder', FALSE)) {
31 if (!isset($sandbox['current_paragraph_field_id'])) {
32 $paragraph_field_ids = [];
33 // Get all the entity reference revisions fields.
34 $map = \Drupal::service('entity_field.manager')->getFieldMapByFieldType('entity_reference_revisions');
35 foreach ($map as $entity_type_id => $info) {
36 foreach ($info as $name => $data) {
37 if (FieldStorageConfig::loadByName($entity_type_id, $name)->getSetting('target_type') == 'paragraph') {
38 $paragraph_field_ids[] = "$entity_type_id.$name";
43 if (!$paragraph_field_ids) {
44 // There are no paragraph fields. Return before initializing the sandbox.
48 // Initialize the sandbox.
49 $sandbox['current_paragraph_field_id'] = 0;
50 $sandbox['paragraph_field_ids'] = $paragraph_field_ids;
51 $sandbox['max'] = count($paragraph_field_ids);
52 $sandbox['progress'] = 0;
55 /** @var \Drupal\field\FieldStorageConfigInterface $field_storage */
56 $field_storage = FieldStorageConfig::load($sandbox['paragraph_field_ids'][$sandbox['current_paragraph_field_id']]);
57 // For revisionable entity types, we load and update all revisions.
58 $target_entity_type = \Drupal::entityTypeManager()->getDefinition($field_storage->getTargetEntityTypeId());
59 if ($target_entity_type->isRevisionable()) {
60 $revision_id = $target_entity_type->getKey('revision');
61 $entity_ids = \Drupal::entityQuery($field_storage->getTargetEntityTypeId())
62 ->condition($field_storage->getName(), NULL, 'IS NOT NULL')
63 ->range($sandbox['progress'], Settings::get('paragraph_limit', 50))
65 ->sort($revision_id, 'ASC')
70 $id = $target_entity_type->getKey('id');
71 $entity_ids = \Drupal::entityQuery($field_storage->getTargetEntityTypeId())
72 ->condition($field_storage->getName(), NULL, 'IS NOT NULL')
73 ->range($sandbox['progress'], Settings::get('paragraph_limit', 50))
78 foreach ($entity_ids as $revision_id => $entity_id) {
79 // For revisionable entity types, we load a specific revision otherwise load
81 if ($target_entity_type->isRevisionable()) {
82 $host_entity = \Drupal::entityTypeManager()
83 ->getStorage($field_storage->getTargetEntityTypeId())
84 ->loadRevision($revision_id);
87 $host_entity = \Drupal::entityTypeManager()
88 ->getStorage($field_storage->getTargetEntityTypeId())
91 foreach ($host_entity->get($field_storage->getName()) as $field_item) {
92 // Skip broken and already updated references (e.g. Nested paragraphs).
93 if ($field_item->entity && empty($field_item->entity->parent_type->value)) {
94 // Set the parent fields and save, ensure that no new revision is
96 $field_item->entity->parent_type = $field_storage->getTargetEntityTypeId();
97 $field_item->entity->parent_id = $host_entity->id();
98 $field_item->entity->parent_field_name = $field_storage->getName();
99 $field_item->entity->setNewRevision(FALSE);
100 $field_item->entity->save();
104 // Continue with the next paragraph_field_id when the loaded entities are less
105 // than paragraph_limit.
106 if (count($entity_ids) < Settings::get('paragraph_limit', 50)) {
107 $sandbox['current_paragraph_field_id']++;
108 $sandbox['progress'] = 0;
111 $sandbox['progress'] += Settings::get('paragraph_limit', 50);
113 // Update #finished, 1 if the the whole update has finished.
114 $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['current_paragraph_field_id'] / $sandbox['max']);