You are here

public function Blocks::convert in Drupal 7 to 8/9 Module Upgrader 8

Performs required conversions.

Parameters

TargetInterface $target: The target module to convert.

Overrides ConverterInterface::convert

File

src/Plugin/DMU/Converter/Blocks.php, line 37

Class

Blocks
Plugin annotation @Converter( id = "blocks", description = @Translation("Converts Drupal 7 blocks to plugins."), hook = { "hook_block_configure", "hook_block_info", "hook_block_save", "hook_block_view" }, fixme =…

Namespace

Drupal\drupalmoduleupgrader\Plugin\DMU\Converter

Code

public function convert(TargetInterface $target) {
  foreach ($this->pluginDefinition['hook'] as $hook) {
    try {
      $blocks = $this
        ->executeHook($target, $hook);
    } catch (\LogicException $e) {
      $this->log
        ->warning($e
        ->getMessage(), [
        'target' => $target
          ->id(),
        'hook' => substr($hook, 5),
      ]);
    }
  }
  $indexer = $target
    ->getIndexer('function');
  foreach ($blocks as $id => $info) {

    // Render the block plugin's shell.
    $render = [
      '#theme' => 'dmu_block',
      '#module' => $target
        ->id(),
      '#class' => $this
        ->toTitleCase(preg_replace('/[^a-zA-Z0-9_]+/', '_', $id)),
      '#block_id' => $id,
      '#block_label' => $info['info'],
      '#configurable' => $indexer
        ->has('block_configure'),
    ];
    $this
      ->writeClass($target, $this
      ->parse($render));
  }

  // Slap a FIXME on hook_block_info(), and on other block hooks which
  // may or may not exist.
  $this
    ->addFixMe($target, 'block_info');
  if ($indexer
    ->has('hook_block_view')) {
    $this
      ->addFixMe($target, 'block_view');
  }
  if ($indexer
    ->has('hook_block_save')) {
    $this
      ->addFixMe($target, 'block_save');
  }
  if ($indexer
    ->has('hook_block_configure')) {
    $this
      ->addFixMe($target, 'block_configure');
  }
}