Same name in other branches
  1. 2.x src/EntityCloneSettingsManager.php \Drupal\entity_clone\EntityCloneSettingsManager

Manage entity clone configuration.

Hierarchy

Expanded class hierarchy of EntityCloneSettingsManager

3 files declare their use of EntityCloneSettingsManager
ContentEntityCloneFormBase.php in src/EntityClone/Content/ContentEntityCloneFormBase.php
EntityCloneForm.php in src/Form/EntityCloneForm.php
EntityCloneSettingsForm.php in src/Form/EntityCloneSettingsForm.php
1 string reference to 'EntityCloneSettingsManager'
entity_clone.services.yml in ./entity_clone.services.yml
entity_clone.services.yml
1 service uses EntityCloneSettingsManager
entity_clone.settings.manager in ./entity_clone.services.yml
Drupal\entity_clone\EntityCloneSettingsManager

File

src/EntityCloneSettingsManager.php, line 13

Namespace

Drupal\entity_clone
View source
class EntityCloneSettingsManager {
    
    /**
     * The entity type manager service.
     *
     * @var \Drupal\Core\Entity\EntityTypeManagerInterface
     */
    protected $entityTypeManager;
    
    /**
     * The entity type bundle info service.
     *
     * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
     */
    protected $entityTypeBundleInfo;
    
    /**
     * The immutable entity clone settings configuration entity.
     *
     * @var \Drupal\Core\Config\ImmutableConfig
     */
    protected $config;
    
    /**
     * The editable entity clone settings configuration entity.
     *
     * @var \Drupal\Core\Config\Config
     */
    protected $editableConfig;
    
    /**
     * EntityCloneSettingsManager constructor.
     *
     * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
     *   The entity type manager service.
     * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
     *   The entity type bundle info service.
     * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
     *   The config factory service.
     */
    public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info, ConfigFactoryInterface $config_factory) {
        $this->entityTypeManager = $entity_type_manager;
        $this->entityTypeBundleInfo = $entity_type_bundle_info;
        $this->config = $config_factory->get('entity_clone.settings');
        $this->editableConfig = $config_factory->getEditable('entity_clone.settings');
    }
    
    /**
     * Get all content entity types.
     *
     * @return \Drupal\Core\Entity\ContentEntityTypeInterface[]
     *   An array containing all content entity types.
     */
    public function getContentEntityTypes() {
        $definitions = $this->entityTypeManager
            ->getDefinitions();
        $ret = [];
        foreach ($definitions as $machine => $type) {
            if ($type instanceof ContentEntityTypeInterface) {
                $ret[$machine] = $type;
            }
        }
        return $ret;
    }
    
    /**
     * Set the entity clone settings.
     *
     * @param array $settings
     *   The settings from the form.
     */
    public function setFormSettings(array $settings) {
        if (isset($settings['table'])) {
            array_walk_recursive($settings['table'], function (&$item) {
                if ($item == '1') {
                    $item = TRUE;
                }
                else {
                    $item = FALSE;
                }
            });
            $this->editableConfig
                ->set('form_settings', $settings['table'])
                ->save();
        }
    }
    
    /**
     * Get the checkbox default value for a given entity type.
     *
     * @param string $entity_type_id
     *   The entity type ID.
     *
     * @return bool
     *   The default value.
     */
    public function getDefaultValue($entity_type_id) {
        $form_settings = $this->config
            ->get('form_settings');
        if (isset($form_settings[$entity_type_id]['default_value'])) {
            return $form_settings[$entity_type_id]['default_value'];
        }
        return FALSE;
    }
    
    /**
     * Get the checkbox disable value for a given entity type.
     *
     * @param string $entity_type_id
     *   The entity type ID.
     *
     * @return bool
     *   The disable value.
     */
    public function getDisableValue($entity_type_id) {
        $form_settings = $this->config
            ->get('form_settings');
        if (isset($form_settings[$entity_type_id]['disable'])) {
            return $form_settings[$entity_type_id]['disable'];
        }
        return FALSE;
    }
    
    /**
     * Get the checkbox hidden value for a given entity type.
     *
     * @param string $entity_type_id
     *   The entity type ID.
     *
     * @return bool
     *   The hidden value.
     */
    public function getHiddenValue($entity_type_id) {
        $form_settings = $this->config
            ->get('form_settings');
        if (isset($form_settings[$entity_type_id]['hidden'])) {
            return $form_settings[$entity_type_id]['hidden'];
        }
        return FALSE;
    }
    
    /**
     * Set the take ownership setting.
     *
     * @param int $setting
     *   The settings from the form.
     */
    public function setTakeOwnershipSettings(int $setting) {
        $this->editableConfig
            ->set('take_ownership', $setting)
            ->save();
    }
    
    /**
     * Get the take ownership settings.
     */
    public function getTakeOwnershipSetting() {
        return $this->config
            ->get('take_ownership') ?? FALSE;
    }
    
    /**
     * Set the take ownership setting.
     *
     * @param int $setting
     *   The settings from the form.
     */
    public function setExcludeClonedSetting(int $setting) {
        $this->editableConfig
            ->set('no_suffix', $setting)
            ->save();
    }
    
    /**
     * Get the take ownership settings.
     */
    public function getExcludeClonedSetting() {
        return $this->config
            ->get('no_suffix') ?? FALSE;
    }

}

Members

Title Sort descending Modifiers Object type Summary
EntityCloneSettingsManager::$config protected property The immutable entity clone settings configuration entity.
EntityCloneSettingsManager::$editableConfig protected property The editable entity clone settings configuration entity.
EntityCloneSettingsManager::$entityTypeBundleInfo protected property The entity type bundle info service.
EntityCloneSettingsManager::$entityTypeManager protected property The entity type manager service.
EntityCloneSettingsManager::getContentEntityTypes public function Get all content entity types.
EntityCloneSettingsManager::getDefaultValue public function Get the checkbox default value for a given entity type.
EntityCloneSettingsManager::getDisableValue public function Get the checkbox disable value for a given entity type.
EntityCloneSettingsManager::getExcludeClonedSetting public function Get the take ownership settings.
EntityCloneSettingsManager::getHiddenValue public function Get the checkbox hidden value for a given entity type.
EntityCloneSettingsManager::getTakeOwnershipSetting public function Get the take ownership settings.
EntityCloneSettingsManager::setExcludeClonedSetting public function Set the take ownership setting.
EntityCloneSettingsManager::setFormSettings public function Set the entity clone settings.
EntityCloneSettingsManager::setTakeOwnershipSettings public function Set the take ownership setting.
EntityCloneSettingsManager::__construct public function EntityCloneSettingsManager constructor.