|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\acquia_cms_site_studio\Facade; |
| 4 | + |
| 5 | +use Drupal\Core\Config\ConfigFactoryInterface; |
| 6 | +use Drupal\Core\DependencyInjection\ContainerInjectionInterface; |
| 7 | +use Drupal\Core\Entity\EntityTypeManagerInterface; |
| 8 | +use Symfony\Component\DependencyInjection\ContainerInterface; |
| 9 | + |
| 10 | +/** |
| 11 | + * Handles the configuration and settings for Acquia Starter Kit Site Studio. |
| 12 | + * |
| 13 | + * @internal |
| 14 | + * This is a totally internal part of Acquia Starter Kits and may be changed |
| 15 | + * in any way, or removed outright, at any time without warning. External |
| 16 | + * code should not use this class! |
| 17 | + */ |
| 18 | +final class ConfigHandlerFacade implements ContainerInjectionInterface { |
| 19 | + |
| 20 | + /** |
| 21 | + * The config factory. |
| 22 | + * |
| 23 | + * @var \Drupal\Core\Config\ConfigFactoryInterface |
| 24 | + */ |
| 25 | + protected $configFactory; |
| 26 | + |
| 27 | + /** |
| 28 | + * The EntityTypeManager service. |
| 29 | + * |
| 30 | + * @var \Drupal\Core\Entity\EntityTypeManagerInterface |
| 31 | + */ |
| 32 | + protected $entityTypeManager; |
| 33 | + |
| 34 | + /** |
| 35 | + * The Module name. |
| 36 | + * |
| 37 | + * @var string |
| 38 | + */ |
| 39 | + protected string $moduleName; |
| 40 | + |
| 41 | + /** |
| 42 | + * ConfigHandlerFacade constructor. |
| 43 | + * |
| 44 | + * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory |
| 45 | + * The config factory. |
| 46 | + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager |
| 47 | + * Used to obtain data from various entity types. |
| 48 | + */ |
| 49 | + public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager) { |
| 50 | + $this->configFactory = $config_factory; |
| 51 | + $this->entityTypeManager = $entity_type_manager; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * {@inheritdoc} |
| 56 | + */ |
| 57 | + public static function create(ContainerInterface $container) { |
| 58 | + return new static( |
| 59 | + $container->get('config.factory'), |
| 60 | + $container->get('entity_type.manager') |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Set module name. |
| 66 | + * |
| 67 | + * @param string $name |
| 68 | + * Module name. |
| 69 | + * |
| 70 | + * @return void |
| 71 | + * Setting the module name. |
| 72 | + */ |
| 73 | + public function setModuleName(string $name): void { |
| 74 | + $this->moduleName = $name; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Function to add default settings of node revision delete. |
| 79 | + * |
| 80 | + * @param array $settings |
| 81 | + * Config settings data. |
| 82 | + * |
| 83 | + * @return void |
| 84 | + * Saving the default settings. |
| 85 | + */ |
| 86 | + public function processConfigSettings(array $settings): void { |
| 87 | + $config = $this->configFactory->getEditable("$this->moduleName.settings"); |
| 88 | + foreach ($settings as $key => $value) { |
| 89 | + $config->set($key, $value); |
| 90 | + } |
| 91 | + $config->save(); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Set third party settings for entity. |
| 96 | + * |
| 97 | + * @param array $entity |
| 98 | + * Entity data. |
| 99 | + * @param array $settings |
| 100 | + * Third party setting data. |
| 101 | + * |
| 102 | + * @return void |
| 103 | + * Saving the third party settings. |
| 104 | + */ |
| 105 | + public function processThirdPartySettings(array $entity, array $settings): void { |
| 106 | + if ($entity) { |
| 107 | + // Get the entity storage with respective entity_type. |
| 108 | + $type = $this->entityTypeManager->getStorage($entity['entity_type']); |
| 109 | + $entityLoad = $type->load($entity['bundle']); |
| 110 | + // @phpstan-ignore-next-line |
| 111 | + $getThirdPartySettings = $entityLoad ? $entityLoad->get('third_party_settings') : NULL; |
| 112 | + // Set the third party settings. |
| 113 | + if (!empty($entityLoad) && !isset($getThirdPartySettings[$this->moduleName])) { |
| 114 | + foreach ($settings as $key => $value) { |
| 115 | + // @phpstan-ignore-next-line |
| 116 | + $entityLoad->setThirdPartySetting($this->moduleName, $key, $value); |
| 117 | + } |
| 118 | + $entityLoad->save(); |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | +} |
0 commit comments