/** * Checks block revision related operations. */ public function testRevisions() { $blocks = $this->blocks; $logs = $this->revisionLogs; foreach ($blocks as $delta => $revision_id) { // Confirm the correct revision text appears. $loaded = entity_revision_load('block_content', $revision_id); // Verify revision log is the same. $this->assertEqual($loaded->getRevisionLog(), $logs[$delta], format_string('Correct log message found for revision !revision', array('!revision' => $loaded->getRevisionId()))); } // Confirm that this is the default revision. $this->assertTrue($loaded->isDefaultRevision(), 'Third block revision is the default one.'); // Make a new revision and set it to not be default. // This will create a new revision that is not "front facing". // Save this as a non-default revision. $loaded->setNewRevision(); $loaded->isDefaultRevision(FALSE); $loaded->body = $this->randomMachineName(8); $loaded->save(); $this->drupalGet('block/' . $loaded->id()); $this->assertNoText($loaded->body->value, 'Revision body text is not present on default version of block.'); // Verify that the non-default revision id is greater than the default // revision id. $default_revision = entity_load('block_content', $loaded->id()); $this->assertTrue($loaded->getRevisionId() > $default_revision->getRevisionId(), 'Revision id is greater than default revision id.'); }
/** * {@inheritdoc} * * Mark the accessible IDs a user can see. We do not unset unaccessible * values, as other may want to act on those values, even if they can * not be accessed. */ public function prepareView(array $entities_items) { $target_ids = array(); $revision_ids = array(); // Collect every possible entity attached to any of the entities. foreach ($entities_items as $items) { foreach ($items as $item) { if (!empty($item->revision_id)) { $revision_ids[] = $item->revision_id; } elseif (!empty($item->target_id)) { $target_ids[] = $item->target_id; } } } $target_type = $this->getFieldSetting('target_type'); $target_entities = array(); if ($target_ids) { $target_entities = entity_load_multiple($target_type, $target_ids); } if ($revision_ids) { // We need to load the revisions one by-one. foreach ($revision_ids as $revision_id) { $target_entity = entity_revision_load($target_type, $revision_id); // Use the revision ID in the key. $identifier = $target_entity->id() . ':' . $revision_id; $target_entities[$identifier] = $target_entity; } } // Iterate through the fieldable entities again to attach the loaded data. foreach ($entities_items as $items) { $rekey = FALSE; foreach ($items as $item) { // If we have a revision ID, the key uses it as well. $identifier = !empty($item->revision_id) ? $item->target_id . ':' . $item->revision_id : $item->target_id; if ($item->target_id !== 0) { if (!isset($target_entities[$identifier])) { // The entity no longer exists, so empty the item. $item->setValue(NULL); $rekey = TRUE; continue; } $item->entity = $target_entities[$identifier]; if (!$item->entity->access('view')) { continue; } } else { // This is an "auto_create" item, just leave the entity in place. } // Mark item as accessible. $item->access = TRUE; } // Rekey the items array if needed. if ($rekey) { $items->filterEmptyItems(); } } }
/** * {@inheritdoc} */ public function getTarget() { if (!isset($this->target) && isset($this->revision_id)) { // If we have a valid reference, return the entity's TypedData adapter. $entity = entity_revision_load($this->getTargetDefinition()->getEntityTypeId(), $this->revision_id); $this->target = isset($entity) ? $entity->getTypedData() : NULL; } return $this->target; }
/** * Executes the revision tests for the given entity type. * * @param string $entity_type * The entity type to run the tests with. */ protected function runRevisionsTests($entity_type) { // Create initial entity. $entity = $this->container->get('entity_type.manager')->getStorage($entity_type)->create(array('name' => 'foo', 'user_id' => $this->webUser->id())); $entity->field_test_text->value = 'bar'; $entity->save(); $names = array(); $texts = array(); $created = array(); $revision_ids = array(); // Create three revisions. $revision_count = 3; for ($i = 0; $i < $revision_count; $i++) { $legacy_revision_id = $entity->revision_id->value; $legacy_name = $entity->name->value; $legacy_text = $entity->field_test_text->value; $entity = entity_load($entity_type, $entity->id->value); $entity->setNewRevision(TRUE); $names[] = $entity->name->value = $this->randomMachineName(32); $texts[] = $entity->field_test_text->value = $this->randomMachineName(32); $created[] = $entity->created->value = time() + $i + 1; $entity->save(); $revision_ids[] = $entity->revision_id->value; // Check that the fields and properties contain new content. $this->assertTrue($entity->revision_id->value > $legacy_revision_id, format_string('%entity_type: Revision ID changed.', array('%entity_type' => $entity_type))); $this->assertNotEqual($entity->name->value, $legacy_name, format_string('%entity_type: Name changed.', array('%entity_type' => $entity_type))); $this->assertNotEqual($entity->field_test_text->value, $legacy_text, format_string('%entity_type: Text changed.', array('%entity_type' => $entity_type))); } for ($i = 0; $i < $revision_count; $i++) { // Load specific revision. $entity_revision = entity_revision_load($entity_type, $revision_ids[$i]); // Check if properties and fields contain the revision specific content. $this->assertEqual($entity_revision->revision_id->value, $revision_ids[$i], format_string('%entity_type: Revision ID matches.', array('%entity_type' => $entity_type))); $this->assertEqual($entity_revision->name->value, $names[$i], format_string('%entity_type: Name matches.', array('%entity_type' => $entity_type))); $this->assertEqual($entity_revision->field_test_text->value, $texts[$i], format_string('%entity_type: Text matches.', array('%entity_type' => $entity_type))); // Check non-revisioned values are loaded. $this->assertTrue(isset($entity_revision->created->value), format_string('%entity_type: Non-revisioned field is loaded.', array('%entity_type' => $entity_type))); $this->assertEqual($entity_revision->created->value, $created[2], format_string('%entity_type: Non-revisioned field value is the same between revisions.', array('%entity_type' => $entity_type))); } // Confirm the correct revision text appears in the edit form. $entity = entity_load($entity_type, $entity->id->value); $this->drupalGet($entity_type . '/manage/' . $entity->id->value . '/edit'); $this->assertFieldById('edit-name-0-value', $entity->name->value, format_string('%entity_type: Name matches in UI.', array('%entity_type' => $entity_type))); $this->assertFieldById('edit-field-test-text-0-value', $entity->field_test_text->value, format_string('%entity_type: Text matches in UI.', array('%entity_type' => $entity_type))); }
/** * fix to get values from nested field collections nested in other field collections * * @param string[] $fields is an array of strings that represents the levels of the nest - so array('content_column', 'column_title') woud be for content title in content column * @param mixed[] $collection is a...it's "drupal magic" which is more like drupal voodoo and the tpls are the pin-cusion dolls linked to your soul and every time you hack them it causes physical pain * @param string $value is a bunch of who-cares * @param mixed $default is null - recently a stackoverflow.com survey listed back-end web devs as the least satisfied with their jobs that is not a joke but right now is making it sad and funny at the same time * @return mixed[] */ function mos_field_collection_nested_values($fields, $collection, $value = 'value', $default = null) { $collection = mos_array_first($collection['entity']['field_collection_item']); $values = array(); $collections = array(); $fields = array_values($fields); foreach ($fields as $key => $field) { $fields[$key] = 'field_' . mos_preg_remove('/^field_/', $field); } foreach (mos_array_get(mos_array_get($collection, $fields[0], array()), '#items', array()) as $item) { //$collections[] = mos_array_first(mos_object_to_array(entity_load('field_collection_item', array($item['value'])))); $collections[] = mos_object_to_array(entity_revision_load('field_collection_item', $item['revision_id'])); } foreach ($collections as $key => $collection) { $values[$key] = array(); foreach (mos_array_get(mos_array_get($collection, $fields[1], $default), 'und', array()) as $key2 => $nested) { $values[$key][$key2] = $nested[$value]; } } return $values; }
/** * Loads all entities contained in the passed-in $results. *. * If the entity belongs to the base table, then it gets stored in * $result->_entity. Otherwise, it gets stored in * $result->_relationship_entities[$relationship_id]; * * @param \Drupal\views\ResultRow[] $results * The result of the SQL query. */ public function loadEntities(&$results) { $entity_information = $this->getEntityTableInfo(); // No entity tables found, nothing else to do here. if (empty($entity_information)) { return; } // Extract all entity types from entity_information. $entity_types = array(); foreach ($entity_information as $info) { $entity_type = $info['entity_type']; if (!isset($entity_types[$entity_type])) { $entity_types[$entity_type] = \Drupal::entityManager()->getDefinition($entity_type); } } // Assemble a list of entities to load. $ids_by_type = array(); foreach ($entity_information as $info) { $relationship_id = $info['relationship_id']; $entity_type = $info['entity_type']; $entity_info = $entity_types[$entity_type]; $id_key = !$info['revision'] ? $entity_info->getKey('id') : $entity_info->getKey('revision'); $id_alias = $this->getFieldAlias($info['alias'], $id_key); foreach ($results as $index => $result) { // Store the entity id if it was found. if (isset($result->{$id_alias}) && $result->{$id_alias} != '') { $ids_by_type[$entity_type][$index][$relationship_id] = $result->$id_alias; } } } // Load all entities and assign them to the correct result row. foreach ($ids_by_type as $entity_type => $ids) { $flat_ids = iterator_to_array(new \RecursiveIteratorIterator(new \RecursiveArrayIterator($ids)), FALSE); // Drupal core currently has no way to load multiple revisions. Sad. if (isset($entity_information[$entity_type]['revision']) && $entity_information[$entity_type]['revision'] === TRUE) { $entities = array(); foreach ($flat_ids as $revision_id) { $entity = entity_revision_load($entity_type, $revision_id); if ($entity) { $entities[$revision_id] = $entity; } } } else { $entities = entity_load_multiple($entity_type, $flat_ids); } foreach ($ids as $index => $relationships) { foreach ($relationships as $relationship_id => $entity_id) { if (isset($entities[$entity_id])) { $entity = $entities[$entity_id]; } else { $entity = NULL; } if ($relationship_id == 'none') { $results[$index]->_entity = $entity; } else { $results[$index]->_relationship_entities[$relationship_id] = $entity; } } } } }
/** * Loads all entities contained in the passed-in $results. *. * If the entity belongs to the base table, then it gets stored in * $result->_entity. Otherwise, it gets stored in * $result->_relationship_entities[$relationship_id]; */ function loadEntities(&$results) { $entity_information = $this->getEntityTableInfo(); // No entity tables found, nothing else to do here. if (empty($entity_information)) { return; } // Assemble a list of entities to load. $ids_by_type = array(); foreach ($entity_information as $entity_type => $info) { $entity_info = \Drupal::entityManager()->getDefinition($entity_type); $id_key = empty($table['revision']) ? $entity_info->getKey('id') : $entity_info->getKey('revision'); $id_alias = $this->getFieldAlias($info['alias'], $id_key); foreach ($results as $index => $result) { // Store the entity id if it was found. if (isset($result->{$id_alias}) && $result->{$id_alias} != '') { $ids_by_type[$entity_type][$index] = $result->{$id_alias}; } } } // Load all entities and assign them to the correct result row. foreach ($ids_by_type as $entity_type => $ids) { $info = $entity_information[$entity_type]; $relationship_id = $info['relationship_id']; // Drupal core currently has no way to load multiple revisions. Sad. if ($info['revision']) { $entities = array(); foreach ($ids as $revision_id) { $entity = entity_revision_load($entity_type, $revision_id); if ($entity) { $entities[$revision_id] = $entity; } } } else { $entities = entity_load_multiple($entity_type, $ids); } foreach ($ids as $index => $id) { if (isset($entities[$id])) { $entity = $entities[$id]; } else { $entity = NULL; } if ($relationship_id == 'none') { $results[$index]->_entity = $entity; } else { $results[$index]->_relationship_entities[$relationship_id] = $entity; } } } }
/** * Check if the field translation attached to the entity revision identified * by the passed arguments were correctly stored. */ private function checkTranslationRevisions($id, $revision_id, $available_langcodes) { $field_name = $this->fieldStorage->getName(); $entity = entity_revision_load($this->entityTypeId, $revision_id); foreach ($available_langcodes as $langcode => $value) { $passed = $entity->getTranslation($langcode)->{$field_name}->value == $value + 1; $this->assertTrue($passed, format_string('The @language translation for revision @revision was correctly stored', array('@language' => $langcode, '@revision' => $entity->getRevisionId()))); } }
public function setRevision($revision_id) { $revision = \entity_revision_load($this->type(), $revision_id); if (!$revision) { return false; } $this->definition = $revision; return true; }