Further modules included.
[yaffs-website] / web / modules / contrib / drupalmoduleupgrader / src / Plugin / DMU / Converter / Blocks.php
1 <?php
2
3 namespace Drupal\drupalmoduleupgrader\Plugin\DMU\Converter;
4
5 use Drupal\drupalmoduleupgrader\ConverterBase;
6 use Drupal\drupalmoduleupgrader\TargetInterface;
7 use Drupal\drupalmoduleupgrader\Utility\StringTransformTrait;
8
9 /**
10  * @Converter(
11  *  id = "blocks",
12  *  description = @Translation("Converts Drupal 7 blocks to plugins."),
13  *  hook = {
14  *    "hook_block_configure",
15  *    "hook_block_info",
16  *    "hook_block_save",
17  *    "hook_block_view"
18  *  },
19  *  fixme = @Translation("hook_!hook is gone in Drupal 8.
20
21 It has been left here by the Drupal Module Upgrader so that you can move its
22 logic into the appropriate block plugins, which should be in the
23 src/Plugin/Block directory. Once all logic is moved into the plugins, delete
24 this hook."),
25  *  documentation = {
26  *    "https://www.drupal.org/node/1880620"
27  *  }
28  * )
29  */
30 class Blocks extends ConverterBase {
31
32   use StringTransformTrait;
33
34   /**
35    * {@inheritdoc}
36    */
37   public function convert(TargetInterface $target) {
38     try {
39       $blocks = $this->executeHook($target, 'block_info');
40     }
41     catch (\LogicException $e) {
42       $this->log->warning($e->getMessage(), [
43         'target' => $target->id(),
44         'hook' => $this->pluginDefinition['hook'],
45       ]);
46       return;
47     }
48
49     $indexer = $target->getIndexer('function');
50
51     foreach ($blocks as $id => $info) {
52       // Render the block plugin's shell.
53       $render = [
54         '#theme' => 'dmu_block',
55         '#module' => $target->id(),
56         '#class' => $this->toTitleCase(preg_replace('/[^a-zA-Z0-9_]+/', '_', $id)),
57         '#block_id' => $id,
58         '#block_label' => $info['info'],
59         '#configurable' => $indexer->has('block_configure'),
60       ];
61       $this->writeClass($target, $this->parse($render));
62     }
63
64     // Slap a FIXME on hook_block_info(), and on other block hooks which
65     // may or may not exist.
66     $this->addFixMe($target, 'block_info');
67
68     if ($indexer->has('hook_block_view')) {
69       $this->addFixMe($target, 'block_view');
70     }
71     if ($indexer->has('hook_block_save')) {
72       $this->addFixMe($target, 'block_save');
73     }
74     if ($indexer->has('hook_block_configure')) {
75       $this->addFixMe($target, 'block_configure');
76     }
77   }
78
79   /**
80    * Slaps a translated FIXME notice above a block-related hook.
81    *
82    * @param TargetInterface $target
83    *  The target module.
84    * @param string $hook
85    *  The hook to put the FIXME on. It's up to the calling code to ensure
86    *  that the hook actually exists.
87    */
88   private function addFixMe(TargetInterface $target, $hook) {
89     $variables = ['!hook' => $hook];
90
91     $function = $target
92       ->getIndexer('function')
93       ->get('hook_' . $hook)
94       ->setDocComment($this->buildFixMe(NULL, $variables, self::DOC_COMMENT));
95
96     $target->save($function);
97   }
98
99 }