/
LinkHooks.php
116 lines (98 loc) · 3.32 KB
/
LinkHooks.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
declare(strict_types=1);
namespace Drush\Commands\core;
use Consolidation\AnnotatedCommand\AnnotationData;
use Consolidation\AnnotatedCommand\Hooks\HookManager;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\link\LinkItemInterface;
use Drush\Attributes as CLI;
use Drush\Commands\DrushCommands;
use Drush\Commands\field\FieldCreateCommands;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\DependencyInjection\ContainerInterface;
final class LinkHooks extends DrushCommands
{
public function __construct(
protected ModuleHandlerInterface $moduleHandler
) {
}
public static function create(ContainerInterface $container): self
{
$commandHandler = new static(
$container->get('module_handler')
);
return $commandHandler;
}
#[CLI\Hook(type: HookManager::OPTION_HOOK, target: FieldCreateCommands::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.'
);
}
#[CLI\Hook(type: HookManager::ON_EVENT, target: '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()
);
}
#[CLI\Hook(type: HookManager::ON_EVENT, target: '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');
}
}