/** * Builds a table row for overview form. * * @param \Drupal\Core\Config\Entity\ConfigEntityInterface $entity * Data needed to build the list row. * * @return array * A single table row for the overview. */ public function overviewRow(ConfigEntityInterface $entity) { $label = $entity->label() ?: $this->t('@type: @id', array('@type' => $entity->getEntityTypeId(), '@id' => $entity->id())); // Get current job items for the entity to determine translation statuses. $source_lang = $entity->language()->getId(); $current_job_items = tmgmt_job_item_load_latest('config', $entity->getEntityTypeId(), $entity->getConfigDependencyName(), $source_lang); $row['id'] = $entity->id(); $definition = \Drupal::entityTypeManager()->getDefinition($entity->bundle()); $row['config_id'] = $definition->getConfigPrefix() . '.' . $entity->id(); if ($entity->hasLinkTemplate('edit-form')) { $row['title'] = $entity->toLink($label, 'edit-form'); } else { // If the entity doesn't have a link we display a label. $row['title'] = $label; } // Load entity translation specific data. foreach (\Drupal::languageManager()->getLanguages() as $langcode => $language) { $translation_status = 'current'; if ($langcode == $source_lang) { $translation_status = 'original'; } elseif (!$this->isTranslated($langcode, $entity->getConfigDependencyName())) { $translation_status = 'missing'; } // @todo Find a way to support marking configuration translations as outdated. $build = $this->buildTranslationStatus($translation_status, isset($current_job_items[$langcode]) ? $current_job_items[$langcode] : NULL); $row['langcode-' . $langcode] = ['data' => \Drupal::service('renderer')->render($build), 'class' => array('langstatus-' . $langcode)]; } return $row; }
/** * {@inheritdoc} */ protected function getConfigNamesToDelete(ConfigEntityInterface $entity) { /** @var \Drupal\field\FieldStorageConfigInterface $field_storage */ $field_storage = $entity->getFieldStorageDefinition(); $config_names = [$entity->getConfigDependencyName()]; // If there is only one bundle left for this field storage, it will be // deleted too, notify the user about dependencies. if (count($field_storage->getBundles()) <= 1) { $config_names[] = $field_storage->getConfigDependencyName(); } return $config_names; }
/** * Tests a tracker with a dependency that gets removed. * * @param bool $remove_dependency * Whether to remove the dependency from the tracker when the object * depended on is deleted. * * @dataProvider dependencyTestDataProvider */ public function testTrackerDependency($remove_dependency) { // Set the tracker for the index and save it. The tracker configuration // contains the dependencies it will return – in our case, we use the test // server. $dependency_key = $this->dependency->getConfigDependencyKey(); $dependency_name = $this->dependency->getConfigDependencyName(); $tracker = \Drupal::getContainer()->get('plugin.manager.search_api.tracker')->createInstance('search_api_test_dependencies', array($dependency_key => array($dependency_name))); $this->index->setTracker($tracker); $this->index->save(); // Check the dependencies were calculated correctly. $dependencies = $this->index->getDependencies(); $this->assertContains($dependency_name, $dependencies[$dependency_key], 'Tracker dependency correctly inserted'); // Set our magic state key to let the test plugin know whether the // dependency should be removed or not. See // \Drupal\search_api_test_dependencies\Plugin\search_api\tracker\TestTracker::onDependencyRemoval(). $key = 'search_api_test_dependencies.tracker.remove'; \Drupal::state()->set($key, $remove_dependency); // If the index resets the tracker, it needs to know the ID of the default // tracker. if (!$remove_dependency) { \Drupal::configFactory()->getEditable('search_api.settings')->set('default_tracker', 'default')->save(); } // Delete the tracker's dependency. $this->dependency->delete(); // Reload the index and check it's still there. $this->reloadIndex(); $this->assertInstanceOf('Drupal\\search_api\\IndexInterface', $this->index, 'Index not removed'); // Make sure the dependency has been removed, one way or the other. $dependencies = $this->index->getDependencies(); $dependencies += array($dependency_key => array()); $this->assertNotContains($dependency_name, $dependencies[$dependency_key], 'Tracker dependency removed from index'); // Depending on whether the plugin should have removed the dependency or // not, make sure the right action was taken. $tracker_instance = $this->index->getTrackerInstance(); $tracker_id = $tracker_instance->getPluginId(); $tracker_config = $tracker_instance->getConfiguration(); if ($remove_dependency) { $this->assertEquals('search_api_test_dependencies', $tracker_id, 'Tracker not reset'); $this->assertEmpty($tracker_config, 'Tracker settings adapted'); } else { $this->assertEquals('default', $tracker_id, 'Tracker was reset'); $this->assertEmpty($tracker_config, 'Tracker settings were cleared'); } }
/** * Calls an entity's onDependencyRemoval() method. * * A helper method to call onDependencyRemoval() with the correct list of * affected entities. This list should only contain dependencies on the * entity. Configuration and content entity dependencies will be converted * into entity objects. * * @param \Drupal\Core\Config\Entity\ConfigEntityInterface $entity * The entity to call onDependencyRemoval() on. * @param \Drupal\Core\Config\Entity\ConfigEntityInterface[] $dependent_entities * The list of dependent configuration entities. * @param string $type * The type of dependency being checked. Either 'module', 'theme', 'config' * or 'content'. * @param array $names * The specific names to check. If $type equals 'module' or 'theme' then it * should be a list of module names or theme names. In the case of 'config' * or 'content' it should be a list of configuration dependency names. * * @return bool * TRUE if the entity has changed as a result of calling the * onDependencyRemoval() method, FALSE if not. */ protected function callOnDependencyRemoval(ConfigEntityInterface $entity, array $dependent_entities, $type, array $names) { $entity_dependencies = $entity->getDependencies(); if (empty($entity_dependencies)) { // No dependent entities nothing to do. return FALSE; } $affected_dependencies = array('config' => array(), 'content' => array(), 'module' => array(), 'theme' => array()); // Work out if any of the entity's dependencies are going to be affected. if (isset($entity_dependencies[$type])) { // Work out which dependencies the entity has in common with the provided // $type and $names. $affected_dependencies[$type] = array_intersect($entity_dependencies[$type], $names); // If the dependencies are entities we need to convert them into objects. if ($type == 'config' || $type == 'content') { $affected_dependencies[$type] = array_map(function ($name) use($type) { if ($type == 'config') { return $this->loadConfigEntityByName($name); } else { // Ignore the bundle. list($entity_type_id, , $uuid) = explode(':', $name); return $this->entityManager->loadEntityByConfigTarget($entity_type_id, $uuid); } }, $affected_dependencies[$type]); } } // Merge any other configuration entities into the list of affected // dependencies if necessary. if (isset($entity_dependencies['config'])) { foreach ($dependent_entities as $dependent_entity) { if (in_array($dependent_entity->getConfigDependencyName(), $entity_dependencies['config'])) { $affected_dependencies['config'][] = $dependent_entity; } } } // Key the entity arrays by config dependency name to make searching easy. foreach (['config', 'content'] as $dependency_type) { $affected_dependencies[$dependency_type] = array_combine(array_map(function ($entity) { return $entity->getConfigDependencyName(); }, $affected_dependencies[$dependency_type]), $affected_dependencies[$dependency_type]); } // Inform the entity. return $entity->onDependencyRemoval($affected_dependencies); }
/** * Returns config names to delete for the deletion confirmation form. * * @param \Drupal\Core\Config\Entity\ConfigEntityInterface $entity * The entity being deleted. * * @return string[] * A list of configuration names that will be deleted by this form. */ protected function getConfigNamesToDelete(ConfigEntityInterface $entity) { return [$entity->getConfigDependencyName()]; }