Skip to content

Commit

Permalink
Add LinkHooks
Browse files Browse the repository at this point in the history
  • Loading branch information
DieterHolvoet committed Nov 25, 2021
1 parent ad3c1ef commit a5a55e0
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
105 changes: 105 additions & 0 deletions src/Drupal/Commands/core/LinkHooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

namespace Drush\Drupal\Commands\core;

use Consolidation\AnnotatedCommand\AnnotationData;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\link\LinkItemInterface;
use Drush\Commands\DrushCommands;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;

class LinkHooks extends DrushCommands
{
/** @var ModuleHandlerInterface */
protected $moduleHandler;

public function __construct(
ModuleHandlerInterface $moduleHandler
) {
$this->moduleHandler = $moduleHandler;
}

/** @hook option field:create */
public function hookOption(Command $command, AnnotationData $annotationData): void
{
if (!$this->isInstalled()) {
return;
}

$command->addOption(
'link-type',
'',
InputOption::VALUE_REQUIRED,
'Allowed link type.'
);

$command->addOption(
'allow-link-text',
'',
InputOption::VALUE_REQUIRED,
'Allow link text.'
);
}

/** @hook on-event field-create-set-options */
public function hookSetOptions(InputInterface $input): void
{
if (
!$this->isInstalled()
|| $input->getOption('field-type') !== 'link'
) {
return;
}

$input->setOption(
'link-type',
$this->input->getOption('link-type') ?? $this->askLinkType()
);

$input->setOption(
'allow-link-text',
$this->input->getOption('allow-link-text') ?? $this->askAllowLinkText()
);
}

/** @hook on-event field-create-field-config */
public function hookFieldConfig(array $values, InputInterface $input): array
{
if (
!$this->isInstalled()
|| $values['field_type'] !== 'link'
) {
return $values;
}

$values['settings']['title'] = $input->getOption('allow-link-text');
$values['settings']['link_type'] = $input->getOption('link-type');

return $values;
}

protected function askLinkType(): int
{
return $this->io()->choice('Allowed link type', [
LinkItemInterface::LINK_INTERNAL => (string) t('Internal links only'),
LinkItemInterface::LINK_EXTERNAL => (string) t('External links only'),
LinkItemInterface::LINK_GENERIC => (string) t('Both internal and external links'),
]);
}

protected function askAllowLinkText(): int
{
return $this->io()->choice('Allow link text', [
DRUPAL_DISABLED => (string) t('Disabled'),
DRUPAL_OPTIONAL => (string) t('Optional'),
DRUPAL_REQUIRED => (string) t('Required'),
]);
}

protected function isInstalled(): bool
{
return $this->moduleHandler->moduleExists('link');
}
}
6 changes: 6 additions & 0 deletions src/Drupal/Commands/core/drush.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ services:
- [ setContentTranslationManager, [ '@?content_translation.manager' ] ]
tags:
- { name: drush.command }
link.hooks:
class: \Drush\Drupal\Commands\core\LinkHooks
arguments:
- '@module_handler'
tags:
- { name: drush.command }
image.commands:
class: \Drush\Drupal\Commands\core\ImageCommands
tags:
Expand Down

0 comments on commit a5a55e0

Please sign in to comment.