You are here

public function VueBlock::build in Decoupled Blocks: Vue.js 8

Builds and returns the renderable array for this block plugin.

If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).

Return value

array A renderable array representing the content of the block.

Overrides PdbBlock::build

See also

\Drupal\block\BlockViewBuilder

File

src/Plugin/Block/VueBlock.php, line 61

Class

VueBlock
Exposes a Vue component as a block.

Namespace

Drupal\pdb_vue\Plugin\Block

Code

public function build() {
  $info = $this
    ->getComponentInfo();
  $machine_name = $info['machine_name'];
  $config = $this->configFactory
    ->get('pdb_vue.settings');
  $template = '';
  $build = parent::build();
  if (isset($config) && $config
    ->get('use_spa') && isset($info['component']) && $info['component']) {

    // Create markup string of component properties.
    $props_string = $this
      ->buildPropertyString();
    $build['#allowed_tags'] = [
      $machine_name,
    ];
    $build['#markup'] = '<' . $machine_name . $props_string . ' instance-id="' . $this->configuration['uuid'] . '"></' . $machine_name . '>';
  }
  else {

    // Use raw HTML if a template is provided.
    if (!empty($info['template'])) {
      $template = file_get_contents($info['path'] . '/' . $info['template']);
    }
    $build['#markup'] = VueMarkup::create('<div class="' . $machine_name . '" id="' . $this->configuration['uuid'] . '">' . $template . '</div>');
  }
  return $build;
}