Chris@0: entityManager = $entity_manager; Chris@0: $this->state = $state; Chris@0: $this->fieldTypeManager = $field_type_manager; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) { Chris@0: return new static( Chris@0: $entity_type, Chris@0: $container->get('config.factory'), Chris@0: $container->get('uuid'), Chris@0: $container->get('language_manager'), Chris@0: $container->get('entity.manager'), Chris@0: $container->get('state'), Chris@0: $container->get('plugin.manager.field.field_type') Chris@0: ); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function importDelete($name, Config $new_config, Config $old_config) { Chris@0: // If the field storage has been deleted in the same import, the field will Chris@0: // be deleted by then, and there is nothing left to do. Just return TRUE so Chris@0: // that the file does not get written to active store. Chris@0: if (!$old_config->get()) { Chris@0: return TRUE; Chris@0: } Chris@0: return parent::importDelete($name, $new_config, $old_config); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function loadByProperties(array $conditions = []) { Chris@0: // Include deleted fields if specified in the $conditions parameters. Chris@0: $include_deleted = isset($conditions['include_deleted']) ? $conditions['include_deleted'] : FALSE; Chris@0: unset($conditions['include_deleted']); Chris@0: Chris@0: $fields = []; Chris@0: Chris@0: // Get fields stored in configuration. If we are explicitly looking for Chris@0: // deleted fields only, this can be skipped, because they will be Chris@0: // retrieved from state below. Chris@0: if (empty($conditions['deleted'])) { Chris@0: if (isset($conditions['entity_type']) && isset($conditions['bundle']) && isset($conditions['field_name'])) { Chris@0: // Optimize for the most frequent case where we do have a specific ID. Chris@0: $id = $conditions['entity_type'] . '.' . $conditions['bundle'] . '.' . $conditions['field_name']; Chris@0: $fields = $this->loadMultiple([$id]); Chris@0: } Chris@0: else { Chris@0: // No specific ID, we need to examine all existing fields. Chris@0: $fields = $this->loadMultiple(); Chris@0: } Chris@0: } Chris@0: Chris@0: // Merge deleted fields (stored in state) if needed. Chris@0: if ($include_deleted || !empty($conditions['deleted'])) { Chris@0: $deleted_fields = $this->state->get('field.field.deleted') ?: []; Chris@0: $deleted_storages = $this->state->get('field.storage.deleted') ?: []; Chris@0: foreach ($deleted_fields as $id => $config) { Chris@0: // If the field storage itself is deleted, inject it directly in the field. Chris@0: if (isset($deleted_storages[$config['field_storage_uuid']])) { Chris@0: $config['field_storage'] = $this->entityManager->getStorage('field_storage_config')->create($deleted_storages[$config['field_storage_uuid']]); Chris@0: } Chris@0: $fields[$id] = $this->create($config); Chris@0: } Chris@0: } Chris@0: Chris@0: // Collect matching fields. Chris@0: $matching_fields = []; Chris@0: foreach ($fields as $field) { Chris@0: // Some conditions are checked against the field storage. Chris@0: $field_storage = $field->getFieldStorageDefinition(); Chris@0: Chris@0: // Only keep the field if it matches all conditions. Chris@0: foreach ($conditions as $key => $value) { Chris@0: // Extract the actual value against which the condition is checked. Chris@0: switch ($key) { Chris@0: case 'field_name': Chris@0: $checked_value = $field_storage->getName(); Chris@0: break; Chris@0: Chris@0: case 'field_id': Chris@0: case 'field_storage_uuid': Chris@0: $checked_value = $field_storage->uuid(); Chris@0: break; Chris@0: Chris@0: case 'uuid'; Chris@0: $checked_value = $field->uuid(); Chris@0: break; Chris@0: Chris@0: case 'deleted'; Chris@0: $checked_value = $field->isDeleted(); Chris@0: break; Chris@0: Chris@0: default: Chris@0: $checked_value = $field->get($key); Chris@0: break; Chris@0: } Chris@0: Chris@0: // Skip to the next field as soon as one condition does not match. Chris@0: if ($checked_value != $value) { Chris@0: continue 2; Chris@0: } Chris@0: } Chris@0: Chris@0: // When returning deleted fields, key the results by UUID since they Chris@0: // can include several fields with the same ID. Chris@0: $key = $include_deleted ? $field->uuid() : $field->id(); Chris@0: $matching_fields[$key] = $field; Chris@0: } Chris@0: Chris@0: return $matching_fields; Chris@0: } Chris@0: Chris@0: }