You are here

class YamlFormThirdPartySettingsManager in YAML Form 8

Form third party settings manager.

Hierarchy

Expanded class hierarchy of YamlFormThirdPartySettingsManager

1 string reference to 'YamlFormThirdPartySettingsManager'
yamlform.services.yml in ./yamlform.services.yml
yamlform.services.yml
1 service uses YamlFormThirdPartySettingsManager
yamlform.third_party_settings_manager in ./yamlform.services.yml
Drupal\yamlform\YamlFormThirdPartySettingsManager

File

src/YamlFormThirdPartySettingsManager.php, line 15

Namespace

Drupal\yamlform
View source
class YamlFormThirdPartySettingsManager implements YamlFormThirdPartySettingsManagerInterface {
  use StringTranslationTrait;

  /**
   * The configuration object factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * The module handler to load includes.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * The path validator.
   *
   * @var \Drupal\Core\Path\PathValidatorInterface
   */
  protected $pathValidator;

  /**
   * Add-ons manager.
   *
   * @var \Drupal\yamlform\YamlFormAddonsManagerInterface
   */
  protected $addonsManager;

  /**
   * The YAML Form module's default configuration settings.
   *
   * @var \Drupal\Core\Config\Config
   */
  protected $config;

  /**
   * Constructs a new YamlFormThirdPartySettingsManager.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The configuration object factory.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler class to use for loading includes.
   * @param \Drupal\Core\Path\PathValidatorInterface $path_validator
   *   The path validator.
   * @param \Drupal\yamlform\YamlFormAddonsManagerInterface $addons_manager
   *   The add-ons manager.
   */
  public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler, PathValidatorInterface $path_validator, YamlFormAddonsManagerInterface $addons_manager) {
    $this->configFactory = $config_factory;
    $this->moduleHandler = $module_handler;
    $this->pathValidator = $path_validator;
    $this->addonsManager = $addons_manager;
    $this->config = $this->configFactory
      ->getEditable('yamlform.settings');
    $this
      ->loadIncludes();
  }

  /**
   * Load all third party settings includes.
   *
   * @see {module}/{module}.yamlform.inc
   * @see {module}/yamlform/{module}.yamlform.inc
   * @see yamlform/yamlform.{module}.inc
   */
  protected function loadIncludes() {
    $modules = array_keys($this->moduleHandler
      ->getModuleList());
    foreach ($modules as $module) {
      $this->moduleHandler
        ->loadInclude($module, 'yamlform.inc');
      $this->moduleHandler
        ->loadInclude($module, 'yamlform.inc', "yamlform/{$module}");
      $this->moduleHandler
        ->loadInclude('yamlform', "inc", "third_party_settings/yamlform.{$module}");
    }
  }

  /**
   * {@inheritdoc}
   */
  public function alter($type, &$data, &$context1 = NULL, &$context2 = NULL) {
    $this->moduleHandler
      ->alter($type, $data, $context1, $context2);
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['third_party_settings'] = [
      '#tree' => TRUE,
    ];
    $form['#after_build'] = [
      [
        $this,
        'afterBuild',
      ],
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function afterBuild(array $form, FormStateInterface $form_state) {

    // If third party settings are empty.
    if (!isset($form['third_party_settings']) || !Element::children($form['third_party_settings'])) {

      // Hide all actions including the 'Save configuration' button.
      $form['actions']['#access'] = FALSE;

      // Display a warning.
      drupal_set_message($this
        ->t('There are no third party settings available. Please install a contributed module that integrates with the YAML Form module.'), 'warning');

      // Link to supported Third party settings modules.
      $form['supported'] = [
        'title' => [
          '#markup' => $this
            ->t('Supported modules.'),
          '#prefix' => '<h3>',
          '#suffix' => '</h3>',
        ],
        'modules' => [
          '#theme' => 'admin_block_content',
          '#content' => $this->addonsManager
            ->getThirdPartySettings(),
        ],
      ];
    }
    else {
      ksort($form['third_party_settings']);
    }
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function setThirdPartySetting($module, $key, $value) {
    $this->config
      ->set("third_party_settings.{$module}.{$key}", $value);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getThirdPartySetting($module, $key, $default = NULL) {
    $value = $this->config
      ->get("third_party_settings.{$module}.{$key}");
    return isset($value) ? $value : $default;
  }

  /**
   * {@inheritdoc}
   */
  public function getThirdPartySettings($module) {
    $this->config
      ->get("third_party_settings.{$module}") ?: [];
  }

  /**
   * {@inheritdoc}
   */
  public function unsetThirdPartySetting($module, $key) {
    $this->config
      ->clear("third_party_settings.{$module}.{$key}");

    // If the third party is no longer storing any information, completely
    // remove the array holding the settings for this module.
    if (!$this->config
      ->get("third_party_settings.{$module}")) {
      $this->config
        ->clear("third_party_settings.{$module}");
    }
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getThirdPartyProviders() {
    $third_party_settings = $this->config
      ->get('third_party_settings') ?: [];
    return array_keys($third_party_settings);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
YamlFormThirdPartySettingsManager::$addonsManager protected property Add-ons manager.
YamlFormThirdPartySettingsManager::$config protected property The YAML Form module's default configuration settings.
YamlFormThirdPartySettingsManager::$configFactory protected property The configuration object factory.
YamlFormThirdPartySettingsManager::$moduleHandler protected property The module handler to load includes.
YamlFormThirdPartySettingsManager::$pathValidator protected property The path validator.
YamlFormThirdPartySettingsManager::afterBuild public function Form element #after_build callback: Checks for 'third_party_settings'. Overrides YamlFormThirdPartySettingsManagerInterface::afterBuild
YamlFormThirdPartySettingsManager::alter public function Wrapper for \Drupal\Core\Extension\ModuleHandlerInterface::alter. Overrides YamlFormThirdPartySettingsManagerInterface::alter
YamlFormThirdPartySettingsManager::buildForm public function Third party settings form constructor. Overrides YamlFormThirdPartySettingsManagerInterface::buildForm
YamlFormThirdPartySettingsManager::getThirdPartyProviders public function Gets the list of third parties that store information. Overrides ThirdPartySettingsInterface::getThirdPartyProviders
YamlFormThirdPartySettingsManager::getThirdPartySetting public function Gets the value of a third-party setting. Overrides ThirdPartySettingsInterface::getThirdPartySetting
YamlFormThirdPartySettingsManager::getThirdPartySettings public function Gets all third-party settings of a given module. Overrides ThirdPartySettingsInterface::getThirdPartySettings
YamlFormThirdPartySettingsManager::loadIncludes protected function Load all third party settings includes.
YamlFormThirdPartySettingsManager::setThirdPartySetting public function Sets the value of a third-party setting. Overrides ThirdPartySettingsInterface::setThirdPartySetting
YamlFormThirdPartySettingsManager::unsetThirdPartySetting public function Unsets a third-party setting. Overrides ThirdPartySettingsInterface::unsetThirdPartySetting
YamlFormThirdPartySettingsManager::__construct public function Constructs a new YamlFormThirdPartySettingsManager.