diff --git a/modules/custom/az_seo/drush.services:.yml b/modules/custom/az_seo/drush.services:.yml new file mode 100644 index 0000000000..86bc8da849 --- /dev/null +++ b/modules/custom/az_seo/drush.services:.yml @@ -0,0 +1,5 @@ +services: + az_seo.commands: + class: \Drupal\az_seo\Drush\Commands\AZMetatagSetCommand + tags: + - { name: drush.command } diff --git a/modules/custom/az_seo/src/Drush/Commands/AZMetatagSetCommand.php b/modules/custom/az_seo/src/Drush/Commands/AZMetatagSetCommand.php new file mode 100644 index 0000000000..49b5d33f4e --- /dev/null +++ b/modules/custom/az_seo/src/Drush/Commands/AZMetatagSetCommand.php @@ -0,0 +1,140 @@ +configFactory; + } + + public function __construct( + // @todo remove unnecessary services. + protected ConfigFactoryInterface $configFactory, + #[Autowire(service: 'config.storage')] + protected StorageInterface $configStorage, + protected SiteAliasManagerInterface $siteAliasManager, + protected StorageManagerInterface $configStorageExport, + protected ImportStorageTransformer $importStorageTransformer, + ) { + parent::__construct(); + } + + /** + * Save a global metatag default directly. + */ + #[CLI\Command(name: self::SET, aliases: ['azm', 'azm-set'])] + #[CLI\Argument(name: 'tag', description: 'The tag to set.')] + #[CLI\Argument(name: 'value', description: 'The value to assign to the tag. Use - to read from stdin.')] + #[CLI\Option(name: 'input-format', + description: 'Format to parse the object. Recognized values: string, yaml. Since JSON is a subset of YAML, $value may be in JSON format.', + suggestedValues: ['string', 'json', + ])] + #[CLI\Usage(name: 'drush az-seo:tag schema_organization_name sitename', description: 'Sets a global metatag default of sitename for the schema_organization_name tag.')] + #[CLI\Usage(name: 'drush az-seo:tag schema_organization_parent_organization.@id https://quickstart.arizona.edu', description: 'Sets a global metatag default of https://quickstart.arizona.edu for the @id element of the schema_organization_parent_organization tag.')] + public function set($tag, $value, $options = ['input-format' => 'string']) { + $data = $value; + + if (!isset($data)) { + throw new \Exception(dt('No tag value specified.')); + } + + // Special flag indicating that the value has been passed via STDIN. + if ($data === '-') { + $data = $this->stdin()->contents(); + } + + // Special handling for null. + if (strtolower($data) === 'null') { + $data = NULL; + } + + // Special handling for empty array. + if ($data == '[]') { + $data = []; + } + + if ($options['input-format'] === 'yaml') { + $parser = new Parser(); + $data = $parser->parse($data); + } + + // @todo make class constant. + $config_name = 'metatag.metatag_defaults.global'; + $config = $this->getConfigFactory()->getEditable($config_name); + // @todo determine special handling of parent organization + // Keep track if we need a specific child element of this tag. + $tag_key = NULL; + // Special handling for parent organization. + if (preg_match('/^schema_organization_parent_organization\.(.+)/', $tag, $matches)) { + $tag = 'schema_organization_parent_organization'; + $tag_key = $matches[1]; + } + // Check to see if tag already exists. + $tag = 'tags.' . $tag; + $existing_tag = $config->get($tag); + // Special case handling, unserialize organization. + if ($tag_key) { + // @todo more sanity checking. + $nested = unserialize($existing_tag, ['allowed_classes' => FALSE]); + if (is_array($nested)) { + // Set the requested nested array element. + $nested[$tag_key] = $data; + } + $data = $nested; + } + // Parent organization needs to be serialized for storage. + if ($tag === 'tags.schema_organization_parent_organization') { + $data = serialize($data); + } + $simulate = $this->getConfig()->simulate(); + + $confirmed = FALSE; + if ($config->isNew() && $this->io()->confirm(dt('!name config does not exist. Do you want to create a new config object?', ['!name' => $config_name]))) { + $confirmed = TRUE; + } + elseif (($existing_tag === NULL) && $this->io()->confirm(dt('Tag !tag does not exist. Do you want to create the tag?', ['!tag' => $tag]))) { + $confirmed = TRUE; + } + elseif ($this->io()->confirm(dt('Do you want to update the !tag tag in !name config?', [ + '!tag' => $tag, + '!name' => $config_name, + ]))) { + $confirmed = TRUE; + } + if ($confirmed && !$simulate) { + return $config->set($tag, $data)->save(); + } + } + +}