/** * {@inheritdoc} */ public function execute() { // Load the relevant records. $records = $this->keyValueFactory->get('entity_storage__' . $this->entityTypeId)->getAll(); // Apply conditions. $result = $this->condition->compile($records); // Apply sort settings. foreach ($this->sort as $sort) { $direction = $sort['direction'] == 'ASC' ? -1 : 1; $field = $sort['field']; uasort($result, function ($a, $b) use($field, $direction) { return $a[$field] <= $b[$field] ? $direction : -$direction; }); } // Let the pager do its work. $this->initializePager(); if ($this->range) { $result = array_slice($result, $this->range['start'], $this->range['length'], TRUE); } if ($this->count) { return count($result); } // Create the expected structure of entity_id => entity_id. $entity_ids = array_keys($result); return array_combine($entity_ids, $entity_ids); }
/** * {@inheritdoc} */ protected function interact(InputInterface $input, OutputInterface $output) { $io = new DrupalStyle($input, $output); $name = $input->getArgument('name'); if (!$name) { $names = array_keys($this->keyValue->get('state')->getAll()); $name = $io->choiceNoList($this->trans('commands.state.delete.arguments.name'), $names); $input->setArgument('name', $name); } }
/** * Constructs a UpdateManager. * * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * The config factory. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler * The Module Handler service * @param \Drupal\update\UpdateProcessorInterface $update_processor * The Update Processor service. * @param \Drupal\Core\StringTranslation\TranslationInterface $translation * The translation service. * @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $key_value_expirable_factory * The expirable key/value factory. */ public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler, UpdateProcessorInterface $update_processor, TranslationInterface $translation, KeyValueFactoryInterface $key_value_expirable_factory) { $this->updateSettings = $config_factory->get('update.settings'); $this->moduleHandler = $module_handler; $this->updateProcessor = $update_processor; $this->stringTranslation = $translation; $this->keyValueStore = $key_value_expirable_factory->get('update'); $this->availableReleasesTempStore = $key_value_expirable_factory->get('update_available_releases'); $this->projects = array(); }
/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $io = new DrupalStyle($input, $output); $key = $input->getArgument('key'); if ($key) { $io->info($key); $io->writeln(Yaml::encode($this->state->get($key))); return 0; } $tableHeader = [$this->trans('commands.state.debug.messages.key')]; $keyStoreStates = array_keys($this->keyValue->get('state')->getAll()); $io->table($tableHeader, $keyStoreStates); return 0; }
/** * {@inheritdoc} */ protected function interact(InputInterface $input, OutputInterface $output) { $io = new DrupalStyle($input, $output); $key = $input->getArgument('key'); $value = $input->getArgument('value'); if (!$key) { $names = array_keys($this->keyValue->get('state')->getAll()); $key = $io->choiceNoList($this->trans('commands.state.override.arguments.key'), $names); $input->setArgument('key', $key); } if (!$value) { $value = $io->ask($this->trans('commands.state.override.arguments.value')); $input->setArgument('value', $value); } }
/** * {@inheritdoc} */ public function onFieldDefinitionDelete(FieldDefinitionInterface $field_definition) { $entity_type_id = $field_definition->getTargetEntityTypeId(); $bundle = $field_definition->getTargetBundle(); $field_name = $field_definition->getName(); // Notify the storage about the field deletion. $this->entityTypeManager->getStorage($entity_type_id)->onFieldDefinitionDelete($field_definition); // Unset the bundle from the bundle field map key value collection. $bundle_field_map = $this->keyValueFactory->get('entity.definitions.bundle_field_map')->get($entity_type_id); unset($bundle_field_map[$field_name]['bundles'][$bundle]); if (empty($bundle_field_map[$field_name]['bundles'])) { // If there are no bundles left, remove the field from the map. unset($bundle_field_map[$field_name]); } $this->keyValueFactory->get('entity.definitions.bundle_field_map')->set($entity_type_id, $bundle_field_map); // Delete the cache entry. $this->cacheBackend->delete('entity_field_map'); // If the field map is initialized, update it as well, so that calls to it // do not have to rebuild it again. if ($field_map = $this->entityFieldManager->getFieldMap()) { unset($field_map[$entity_type_id][$field_name]['bundles'][$bundle]); if (empty($field_map[$entity_type_id][$field_name]['bundles'])) { unset($field_map[$entity_type_id][$field_name]); } $this->entityFieldManager->setFieldMap($field_map); } }
/** * @covers ::onFieldDefinitionDelete */ public function testOnFieldDefinitionDeleteSingleBundles() { $field_definition = $this->prophesize(FieldDefinitionInterface::class); $field_definition->getTargetEntityTypeId()->willReturn('test_entity_type'); $field_definition->getTargetBundle()->willReturn('test_bundle'); $field_definition->getName()->willReturn('test_field'); $storage = $this->prophesize(DynamicallyFieldableEntityStorageInterface::class); $storage->onFieldDefinitionDelete($field_definition->reveal())->shouldBeCalledTimes(1); $this->entityTypeManager->getStorage('test_entity_type')->willReturn($storage->reveal()); $entity = $this->prophesize(EntityTypeInterface::class); $this->setUpEntityManager(['test_entity_type' => $entity]); // Set up the stored bundle field map. $key_value_store = $this->prophesize(KeyValueStoreInterface::class); $this->keyValueFactory->get('entity.definitions.bundle_field_map')->willReturn($key_value_store->reveal()); $key_value_store->get('test_entity_type')->willReturn(['test_field' => ['type' => 'test_type', 'bundles' => ['test_bundle' => 'test_bundle', 'second_bundle' => 'second_bundle']]]); $key_value_store->set('test_entity_type', ['test_field' => ['type' => 'test_type', 'bundles' => ['second_bundle' => 'second_bundle']]])->shouldBeCalled(); $this->fieldDefinitionListener->onFieldDefinitionDelete($field_definition->reveal()); }
/** * {@inheritdoc} */ public function getFieldMap() { if (!$this->fieldMap) { // Not prepared, try to load from cache. $cid = 'entity_field_map'; if ($cache = $this->cacheGet($cid)) { $this->fieldMap = $cache->data; } else { // The field map is built in two steps. First, add all base fields, by // looping over all fieldable entity types. They always exist for all // bundles, and we do not expect to have so many different entity // types for this to become a bottleneck. foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $entity_type) { if ($entity_type->isSubclassOf(FieldableEntityInterface::class)) { $bundles = array_keys($this->entityTypeBundleInfo->getBundleInfo($entity_type_id)); foreach ($this->getBaseFieldDefinitions($entity_type_id) as $field_name => $base_field_definition) { $this->fieldMap[$entity_type_id][$field_name] = ['type' => $base_field_definition->getType(), 'bundles' => array_combine($bundles, $bundles)]; } } } // In the second step, the per-bundle fields are added, based on the // persistent bundle field map stored in a key value collection. This // data is managed in the EntityManager::onFieldDefinitionCreate() // and EntityManager::onFieldDefinitionDelete() methods. Rebuilding this // information in the same way as base fields would not scale, as the // time to query would grow exponentially with more fields and bundles. // A cache would be deleted during cache clears, which is the only time // it is needed, so a key value collection is used. $bundle_field_maps = $this->keyValueFactory->get('entity.definitions.bundle_field_map')->getAll(); foreach ($bundle_field_maps as $entity_type_id => $bundle_field_map) { foreach ($bundle_field_map as $field_name => $map_entry) { if (!isset($this->fieldMap[$entity_type_id][$field_name])) { $this->fieldMap[$entity_type_id][$field_name] = $map_entry; } else { $this->fieldMap[$entity_type_id][$field_name]['bundles'] += $map_entry['bundles']; } } } $this->cacheSet($cid, $this->fieldMap, Cache::PERMANENT, ['entity_types']); } } return $this->fieldMap; }
/** * @covers ::getFieldMapByFieldType */ public function testGetFieldMapByFieldType() { // Set up a content entity type. $entity_type = $this->prophesize(ContentEntityTypeInterface::class); $entity_class = EntityManagerTestEntity::class; // Set up the entity type bundle info to return two bundles for the // fieldable entity type. $this->entityTypeBundleInfo->getBundleInfo('test_entity_type')->willReturn(['first_bundle' => 'first_bundle', 'second_bundle' => 'second_bundle'])->shouldBeCalled(); $this->moduleHandler->getImplementations('entity_base_field_info')->willReturn([])->shouldBeCalled(); // Define an ID field definition as a base field. $id_definition = $this->prophesize(FieldDefinitionInterface::class); $id_definition->getType()->willReturn('integer')->shouldBeCalled(); $base_field_definitions = ['id' => $id_definition->reveal()]; $entity_class::$baseFieldDefinitions = $base_field_definitions; // Set up the stored bundle field map. $key_value_store = $this->prophesize(KeyValueStoreInterface::class); $this->keyValueFactory->get('entity.definitions.bundle_field_map')->willReturn($key_value_store->reveal())->shouldBeCalled(); $key_value_store->getAll()->willReturn(['test_entity_type' => ['by_bundle' => ['type' => 'string', 'bundles' => ['second_bundle' => 'second_bundle']]]])->shouldBeCalled(); // Mock the base field definition override. $override_entity_type = $this->prophesize(EntityTypeInterface::class); $this->setUpEntityTypeDefinitions(['test_entity_type' => $entity_type, 'base_field_override' => $override_entity_type]); $entity_type->getClass()->willReturn($entity_class)->shouldBeCalled(); $entity_type->getKeys()->willReturn(['default_langcode' => 'default_langcode'])->shouldBeCalled(); $entity_type->isSubclassOf(FieldableEntityInterface::class)->willReturn(TRUE)->shouldBeCalled(); $entity_type->isTranslatable()->shouldBeCalled(); $entity_type->getProvider()->shouldBeCalled(); $override_entity_type->isSubclassOf(FieldableEntityInterface::class)->willReturn(FALSE)->shouldBeCalled(); $integerFields = $this->entityFieldManager->getFieldMapByFieldType('integer'); $this->assertCount(1, $integerFields['test_entity_type']); $this->assertArrayNotHasKey('non_fieldable', $integerFields); $this->assertArrayHasKey('id', $integerFields['test_entity_type']); $this->assertArrayNotHasKey('by_bundle', $integerFields['test_entity_type']); $stringFields = $this->entityFieldManager->getFieldMapByFieldType('string'); $this->assertCount(1, $stringFields['test_entity_type']); $this->assertArrayNotHasKey('non_fieldable', $stringFields); $this->assertArrayHasKey('by_bundle', $stringFields['test_entity_type']); $this->assertArrayNotHasKey('id', $stringFields['test_entity_type']); }
/** * Constructs a State object. * * @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $key_value_factory * The key value store to use. */ function __construct(KeyValueFactoryInterface $key_value_factory) { $this->keyValueStore = $key_value_factory->get('state'); }
/** * Stores the entity type's field storage definitions in the application state. * * @param string $entity_type_id * The entity type identifier. * @param \Drupal\Core\Field\FieldStorageDefinitionInterface[] $storage_definitions * An array of field storage definitions. */ protected function setLastInstalledFieldStorageDefinitions($entity_type_id, array $storage_definitions) { $this->keyValueFactory->get('entity.definitions.installed')->set($entity_type_id . '.field_storage_definitions', $storage_definitions); }
/** * Constructs a UpdateProcessor. * * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * The config factory. * @param \Drupal\Core\Queue\QueueFactory $queue_factory * The queue factory * @param \Drupal\update\UpdateFetcherInterface $update_fetcher * The update fetcher service * @param \Drupal\Core\State\StateInterface $state_store * The state service. * @param \Drupal\Core\PrivateKey $private_key * The private key factory service. * @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $key_value_factory * The key/value factory. * @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $key_value_expirable_factory * The expirable key/value factory. */ public function __construct(ConfigFactoryInterface $config_factory, QueueFactory $queue_factory, UpdateFetcherInterface $update_fetcher, StateInterface $state_store, PrivateKey $private_key, KeyValueFactoryInterface $key_value_factory, KeyValueFactoryInterface $key_value_expirable_factory) { $this->updateFetcher = $update_fetcher; $this->updateSettings = $config_factory->get('update.settings'); $this->fetchQueue = $queue_factory->get('update_fetch_tasks'); $this->tempStore = $key_value_expirable_factory->get('update'); $this->fetchTaskStore = $key_value_factory->get('update_fetch_task'); $this->availableReleasesTempStore = $key_value_expirable_factory->get('update_available_releases'); $this->stateStore = $state_store; $this->privateKey = $private_key; $this->fetchTasks = array(); $this->failed = array(); }
/** * @covers ::onFieldDefinitionDelete */ public function testOnFieldDefinitionDeleteSingleBundles() { $field_definition = $this->prophesize(FieldDefinitionInterface::class); $field_definition->getTargetEntityTypeId()->willReturn('test_entity_type'); $field_definition->getTargetBundle()->willReturn('test_bundle'); $field_definition->getName()->willReturn('test_field'); $class = $this->getMockClass(DynamicallyFieldableEntityStorageInterface::class); $entity = $this->prophesize(EntityTypeInterface::class); $entity->getHandlerClass('storage')->willReturn($class); $this->setUpEntityManager(array('test_entity_type' => $entity)); // The entity manager will instantiate a new object with the given class // name. Define the mock expectations on that. $storage = $this->entityManager->getStorage('test_entity_type'); $storage->expects($this->once())->method('onFieldDefinitionDelete')->with($field_definition->reveal()); // Set up the stored bundle field map. $key_value_store = $this->prophesize(KeyValueStoreInterface::class); $this->keyValueFactory->get('entity.definitions.bundle_field_map')->willReturn($key_value_store->reveal()); $key_value_store->get('test_entity_type')->willReturn(['test_field' => ['type' => 'test_type', 'bundles' => ['test_bundle' => 'test_bundle', 'second_bundle' => 'second_bundle']]]); $key_value_store->set('test_entity_type', ['test_field' => ['type' => 'test_type', 'bundles' => ['second_bundle' => 'second_bundle']]])->shouldBeCalled(); $this->entityManager->onFieldDefinitionDelete($field_definition->reveal()); }
/** * @param string $uuid * @return \Drupal\Core\KeyValueStore\KeyValueStoreInterface */ protected function keyValueStore($uuid) { $workspace_id = $this->workspaceId ?: $this->workspaceManager->getActiveWorkspace()->id(); return $this->keyValueFactory->get("entity.index.rev.tree.{$workspace_id}.{$uuid}"); }
/** * Constructs a State object. * * @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $key_value_factory * The key value store to use. */ function __construct(KeyValueFactoryInterface $key_value_factory) { $this->keyValueStore = $key_value_factory->get('locale.project'); }
/** * Gets the key value store used to store fast lookups. * * @return \Drupal\Core\KeyValueStore\KeyValueStoreInterface * The key value store used to store fast lookups. */ protected function getConfigKeyStore() { return $this->keyValueFactory->get(QueryFactory::CONFIG_LOOKUP_PREFIX . $this->entityTypeId); }
/** * @covers ::onFieldDefinitionDelete */ public function testonFieldDefinitionDeleteSingleBundles() { $field_definition = $this->getMock('Drupal\\Core\\Field\\FieldDefinitionInterface'); $field_definition->expects($this->atLeastOnce())->method('getTargetEntityTypeId')->willReturn('test_entity_type'); $field_definition->expects($this->atLeastOnce())->method('getTargetBundle')->willReturn('test_bundle'); $field_definition->expects($this->atLeastOnce())->method('getName')->willReturn('test_field'); $storage = $this->getMock('Drupal\\Core\\Entity\\DynamicallyFieldableEntityStorageInterface'); $class = get_class($storage); $entity = $this->getMock('Drupal\\Core\\Entity\\EntityTypeInterface'); $entity->expects($this->once())->method('getHandlerClass')->with('storage')->will($this->returnValue($class)); $this->setUpEntityManager(array('test_entity_type' => $entity)); // The entity manager will instantiate a new object with the given class // name. Define the mock expectations on that. $storage = $this->entityManager->getStorage('test_entity_type'); $storage->expects($this->once())->method('onFieldDefinitionDelete')->with($field_definition); // Set up the stored bundle field map. $key_value_store = $this->getMock('Drupal\\Core\\KeyValueStore\\KeyValueStoreInterface'); $this->keyValueFactory->expects($this->exactly(2))->method('get')->with('entity.definitions.bundle_field_map')->willReturn($key_value_store); $key_value_store->expects($this->once())->method('get')->with('test_entity_type')->willReturn(['test_field' => ['type' => 'test_type', 'bundles' => ['test_bundle' => 'test_bundle', 'second_bundle' => 'second_bundle']]]); $key_value_store->expects($this->once())->method('set')->with('test_entity_type', ['test_field' => ['type' => 'test_type', 'bundles' => ['second_bundle' => 'second_bundle']]]); $this->entityManager->onFieldDefinitionDelete($field_definition); }
/** * @param string $workspace_name * @return \Drupal\Core\KeyValueStore\KeyValueStoreInterface */ protected function keyValueStore($workspace_name) { return $this->keyValueFactory->get($this->collectionPrefix . $workspace_name); }