VariableGet.php in Drupal 7 to 8/9 Module Upgrader 8
File
src/Plugin/DMU/Converter/Functions/VariableGet.php
View source
<?php
namespace Drupal\drupalmoduleupgrader\Plugin\DMU\Converter\Functions;
use Drupal\Component\Serialization\Yaml;
use Drupal\Core\Config\InstallStorage;
use Drupal\drupalmoduleupgrader\TargetInterface;
use Pharborist\Functions\FunctionCallNode;
use Pharborist\Objects\ClassMethodCallNode;
use Pharborist\Types\ScalarNode;
class VariableGet extends VariableAPI {
private $defaults = [];
private $schema = [];
public function rewrite(FunctionCallNode $call, TargetInterface $target) {
if ($this
->tryRewrite($call, $target)) {
$arguments = $call
->getArguments();
$key = $arguments[0]
->toValue();
if ($arguments[1] instanceof ScalarNode) {
$this->defaults[$key] = $arguments[1]
->toValue();
$this->schema[$key]['type'] = gettype($this->defaults[$key]);
}
else {
$comment = <<<END
Could not extract the default value because it is either indeterminate, or
not scalar. You'll need to provide a default value in
config/install/@module.settings.yml and config/schema/@module.schema.yml.
END;
$variables = [
'@module' => $target
->id(),
];
$this
->buildFixMe($comment, $variables)
->prependTo($call
->getStatement());
}
return ClassMethodCallNode::create('\\Drupal', 'config')
->appendArgument($target
->id() . '.settings')
->appendMethodCall('get')
->appendArgument(clone $arguments[0]);
}
}
public function convert(TargetInterface $target) {
parent::convert($target);
if ($this->defaults && $this->schema) {
$group = $target
->id() . '.settings';
$this
->write($target, InstallStorage::CONFIG_INSTALL_DIRECTORY . '/' . $group . '.yml', Yaml::encode($this->defaults));
$this->defaults = [];
$schema = [
$group => [
'type' => 'mapping',
'label' => (string) $this
->t('Settings'),
'mapping' => $this->schema,
],
];
$this
->write($target, InstallStorage::CONFIG_SCHEMA_DIRECTORY . '/' . $target
->id() . '.schema.yml', Yaml::encode($schema));
$this->schema = [];
}
}
}
Classes
Name |
Description |
VariableGet |
Plugin annotation
@Converter(
id = "variable_get",
description = @Translation("Replaces variable_get() calls with Configuration API.")
) |