View source
<?php
namespace Drupal\markdown\Traits;
use Drupal\Component\Utility\DiffArray;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Form\FormStateInterface;
use Drupal\markdown\Util\SortArray;
trait SettingsTrait {
protected function createSettingElement($name, array $element, FormStateInterface $form_state, callable $valueTransformer = NULL) {
$settingName = $name;
$parts = explode('.', $name);
$name = array_pop($parts);
if (!isset($element['#access']) && !$this
->settingExists($settingName)) {
$element['#access'] = FALSE;
}
if (!isset($element['#title'])) {
@trigger_error('Deprecated in markdown:8.x-2.0 and is removed from markdown:3.0.0. No replacement for automatically populating. The #title property must be explicitly specified for locale tools to extract the correct value. See https://www.drupal.org/project/markdown/issues/3142418.', E_USER_DEPRECATED);
$element['#title'] = $this
->t(ucwords(str_replace([
'-',
'_',
], ' ', $name)));
}
if (!isset($element['#default_value'])) {
$value = $form_state
->getValue($name, $this
->getSetting($settingName));
if ($valueTransformer) {
$return = call_user_func($valueTransformer, $value);
if (isset($return)) {
$value = $return;
}
}
$element['#default_value'] = $value;
}
$defaultValue = $this
->getDefaultSetting($settingName);
if (isset($defaultValue)) {
if ($valueTransformer) {
$return = call_user_func($valueTransformer, $defaultValue);
if (isset($return)) {
$defaultValue = $return;
}
}
FormTrait::resetToDefault($element, $name, $defaultValue, $form_state);
}
return [
$name => FormTrait::createElement($element),
];
}
public static function defaultSettings($pluginDefinition) {
return [];
}
public function getDefaultSetting($name) {
$defaultSettings = static::defaultSettings($this
->getPluginDefinition());
$parts = explode('.', $name);
if (count($parts) == 1) {
return isset($defaultSettings[$name]) ? $defaultSettings[$name] : NULL;
}
$value = NestedArray::getValue($defaultSettings, $parts, $key_exists);
return $key_exists ? $value : NULL;
}
public function getSetting($name, $default = NULL) {
$value = $this
->config()
->get("settings.{$name}");
return isset($value) ? $value : $default;
}
public function getSettings($runtime = FALSE, $sorted = TRUE) {
$settings = $this
->config()
->get('settings') ?: [];
if ($sorted && $settings) {
SortArray::recursiveKeySort($settings);
}
return $settings;
}
public function getSettingOverrides($runtime = FALSE, $sorted = TRUE, array $settings = NULL) {
if (!isset($settings)) {
$settings = $this
->getSettings($runtime, FALSE);
}
$overridden = DiffArray::diffAssocRecursive($settings, static::defaultSettings($this
->getPluginDefinition()));
if ($sorted && $overridden) {
SortArray::recursiveKeySort($overridden);
}
return $overridden;
}
public function settingExists($name) {
return array_key_exists($name, static::defaultSettings($this->pluginDefinition));
}
public function settingsKey() {
return NULL;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
}
}