function RulesCommands::export

Exports a single rule configuration, in YAML format.

@command rules:export @interact-rule-names @aliases rexp,rules-export

@codingStandardsIgnoreStart @usage drush rules:export Displays all rules and allows you to select one to export. @usage drush rules:export test_rule > rules.reaction.test_rule.yml Exports the Rule with machine name 'test_rule' and saves it in a .yml file. @usage drush rules:list rule --fields=machine-name --pipe | xargs -I{} sh -c "drush rules:export '{}' > 'rules.reaction.{}.yml'" Exports all Reaction Rules into individual YAML files. @codingStandardsIgnoreEnd

Parameters

string $rule: Rule name (machine id) to export.

Throws

\Exception

File

src/Commands/RulesCommands.php, line 280

Class

RulesCommands
Drush 9+ commands for the Rules module.

Namespace

Drupal\rules\Commands

Code

public function export($rule) {
    // The $rule argument could refer to a Reaction Rule or a Rules Component.
    $config = $this->configStorage
        ->read('rules.reaction.' . $rule);
    if (empty($config)) {
        $config = $this->configStorage
            ->read('rules.component.' . $rule);
        if (empty($config)) {
            // The @interact-rule-names hook returns fully-qualified names.
            $config = $this->configStorage
                ->read($rule);
            if (empty($config)) {
                throw new \Exception(dt('Could not find a Reaction Rule or a Rules Component named @name', [
                    '@name' => $rule,
                ]));
            }
        }
    }
    $this->output
        ->write(Yaml::encode($config), FALSE);
    $this->logger
        ->success(dt('The rule @name has been exported.', [
        '@name' => $rule,
    ]));
}