3 namespace Drupal\drupalmoduleupgrader\Plugin\DMU\Converter\Functions;
5 use Drupal\Component\Serialization\Yaml;
6 use Drupal\Core\Config\InstallStorage;
7 use Drupal\drupalmoduleupgrader\TargetInterface;
8 use Pharborist\Functions\FunctionCallNode;
9 use Pharborist\Objects\ClassMethodCallNode;
10 use Pharborist\Types\ScalarNode;
14 * id = "variable_get",
15 * description = @Translation("Replaces variable_get() calls with Configuration API.")
18 class VariableGet extends VariableAPI {
21 * Default configuration values extracted from rewritten calls to
26 private $defaults = [];
29 * The schema accompanying any extracted default values.
38 public function rewrite(FunctionCallNode $call, TargetInterface $target) {
39 if ($this->tryRewrite($call, $target)) {
40 $arguments = $call->getArguments();
41 $key = $arguments[0]->toValue();
43 if ($arguments[1] instanceof ScalarNode) {
44 // @TODO Couldn't convert() derive the schema from $this->defaults?
45 // That'd be preferable to having yet another state property ($schema)
47 $this->defaults[$key] = $arguments[1]->toValue();
48 $this->schema[$key]['type'] = getType($this->defaults[$key]);
52 Could not extract the default value because it is either indeterminate, or
53 not scalar. You'll need to provide a default value in
54 config/install/@module.settings.yml and config/schema/@module.schema.yml.
56 $variables = [ '@module' => $target->id() ];
57 $this->buildFixMe($comment, $variables)->prependTo($call->getStatement());
60 return ClassMethodCallNode::create('\Drupal', 'config')
61 ->appendArgument($target->id() . '.settings')
62 ->appendMethodCall('get')
63 ->appendArgument(clone $arguments[0]);
70 public function convert(TargetInterface $target) {
71 parent::convert($target);
73 if ($this->defaults && $this->schema) {
74 $group = $target->id() . '.settings';
76 $this->write($target, InstallStorage::CONFIG_INSTALL_DIRECTORY . '/' . $group . '.yml', Yaml::encode($this->defaults));
82 'label' => (string) $this->t('Settings'),
83 'mapping' => $this->schema,
86 $this->write($target, InstallStorage::CONFIG_SCHEMA_DIRECTORY . '/' . $target->id() . '.schema.yml', Yaml::encode($schema));