FeaturesAssignmentBaseType.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace Drupal\features\Plugin\FeaturesAssignment;
  3. use Drupal\component\Utility\Unicode;
  4. use Drupal\features\FeaturesAssignmentMethodBase;
  5. /**
  6. * Class for assigning configuration to packages based on entity types.
  7. *
  8. * @Plugin(
  9. * id = "base",
  10. * weight = -2,
  11. * name = @Translation("Base type"),
  12. * description = @Translation("Use designated types of configuration as the base for configuration package modules. For example, if content types are selected as a base type, a package will be generated for each content type and will include all configuration dependent on that content type."),
  13. * config_route_name = "features.assignment_base",
  14. * default_settings = {
  15. * "types" = {
  16. * "config" = {},
  17. * "content" = {}
  18. * }
  19. * }
  20. * )
  21. */
  22. class FeaturesAssignmentBaseType extends FeaturesAssignmentMethodBase {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function assignPackages($force = FALSE) {
  27. $current_bundle = $this->assigner->getBundle();
  28. $settings = $current_bundle->getAssignmentSettings($this->getPluginId());
  29. $config_base_types = $settings['types']['config'];
  30. $config_types = $this->featuresManager->listConfigTypes();
  31. $config_collection = $this->featuresManager->getConfigCollection();
  32. foreach ($config_collection as $item_name => $item) {
  33. if (in_array($item->getType(), $config_base_types)) {
  34. if (is_null($this->featuresManager->findPackage($item->getShortName())) && !$item->getPackage()) {
  35. $description = $this->t('Provides @label @type and related configuration.', array('@label' => $item->getLabel(), '@type' => Unicode::strtolower($config_types[$item->getType()])));
  36. if (isset($item->getData()['description'])) {
  37. $description .= ' ' . $item->getData()['description'];
  38. }
  39. $this->featuresManager->initPackage($item->getShortName(), $item->getLabel(), $description, 'module', $current_bundle);
  40. // Update list with the package we just added.
  41. try {
  42. $this->featuresManager->assignConfigPackage($item->getShortName(), [$item_name]);
  43. }
  44. catch (\Exception $exception) {
  45. \Drupal::logger('features')->error($exception->getMessage());
  46. }
  47. $this->featuresManager->assignConfigDependents([$item_name]);
  48. }
  49. }
  50. }
  51. $entity_types = $this->entityManager->getDefinitions();
  52. $content_base_types = $settings['types']['content'];
  53. foreach ($content_base_types as $entity_type_id) {
  54. if (!isset($packages[$entity_type_id]) && isset($entity_types[$entity_type_id])) {
  55. $label = $entity_types[$entity_type_id]->getLabel();
  56. $description = $this->t('Provide @label related configuration.', array('@label' => $label));
  57. $this->featuresManager->initPackage($entity_type_id, $label, $description, 'module', $current_bundle);
  58. }
  59. }
  60. }
  61. }