-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
LinkHooks.php
103 lines (87 loc) · 2.87 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
<?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');
}
}