In deze blogpost willen we een aangepaste blokplugin maken met extra configuratie-instellingen.

Voor dit bericht zullen we ons vooral richten op het vertaalbaar maken van de aangepaste pluginconfiguratie.
Er is veel informatie over het maken van aangepaste block plugins, dus daar gaan we niet te diep op in.

Aanmaken van een blok plugin

Maak een MyCustomBlock.php bestand aan bij mymodule/src/Plugin/Block

<?php

namespace Drupal\mymodule\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Provides a custom block.
 *
 * @Block(
 *   id = "my_custom_block",
 *   admin_label = @Translation("My custom block")
 * )
 */
class MyCustomBlock extends BlockBase {

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return array(
      'my_favorite_drink' => $this->t('My favorite drink'),
      );
  }

  /**
   * {@inheritdoc}
   */
  public function blockForm($form, FormStateInterface $form_state) {
    $form['my_favorite_drink'] = array(
      '#type' => 'textfield',
      '#title' => $this->t('My favorite drink'),
      '#description' => $this->t('What would you like to drink?'),
      '#default_value' => $this->configuration['my_favorite_drink'],
    );

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function blockSubmit($form, FormStateInterface $form_state) {
    $this->configuration['my_favorite_drink'] = $form_state->getValue('my_favorite_drink');
  }

  /**
   * {@inheritdoc}
   */
  public function build() {
    $block = array();

    if ($this->configuration['my_favorite_drink']) {
      $block['label'] = array(
        '#type' => 'markup',
        '#markup' => $this->configuration['my_favorite_drink'],
      );
    }

    return $block;
  }
}

 

Configure block

Toevoegen aan een region

Je kan nu je block toevoegen aan een region

No translation

Vertaal de configuratie

Je kan nu proberen het blok te vertalen, maar u zult zien dat alleen de titel beschikbaar is voor de vertaling.

Missing schema

Schema configuratie

Drupals fantastische meertalige systeem maakt gebruik van uw configuraties schema informatie voor het maken van het vertaalbaar.
http://hojtsy.hu/blog/2014-may-19/drupal-8-multilingual-tidbits-15-confi...
http://hojtsy.hu/blog/2014-may-26/drupal-8-multilingual-tidbits-16-confi...

Als we de config_inspector module gebruiken om het schema van ons aangepaste blok te inspecteren, zien we dat de schemadeclaratie voor onze aangepaste eigenschappen ontbreekt.

Schema found

We moeten het declareren bij mymodule/config/schema in een mymodule.schema.yml bestand

block.settings.my_custom_block: type: block_settings label: 'My custom block' mapping: my_favorite_drink: type: label: 'My favorite drink'.

Dit zal ons configuratieschema verklaren.
Het datatype 'label' is een uitbreiding van 'string' en laat het meertalige systeem toe om onze eigenschap te vertalen.

Drupal 8 translate custom block plugin configuration

We kunnen nu de configuratie vertalen op de vertaalpagina van het blok