Paragraph handler allows you to provide "Paragraph Handler" classes that can handle pre-processing and altering the render arrays of Paragraph entities when they are displayed.

This helps to prevent .theme files from getting unruly and allows you to make use of traits and inheritance for your Paragraph display logic.

This module is only useful for developers.

See example below:


namespace Drupal\mymodule\Plugin\ParagraphHandler;

use Drupal\paragraph_handler\Plugin\ParagraphHandlerBase;
use Drupal\Core\Url;

/**
 * Class Card.
 *
 * @ParagraphHandler(
 *   id = "card",
 *   label = @Translation("Card")
 * )
 */
class Card extends ParagraphHandlerBase {
  /**
   * {@inheritdoc}
   */
  public function preprocess(array &$variables, array $element) {
    if ($this->isNested()) {
      $variables['nested_class'] = TRUE;
    }

    $variables['new_variable'] = t('Test');
  }

  /**
   * {@inheritdoc}
   */
  public function build(array &$build) {
    $build['link'] = [
      '#type' => 'link',
      '#title' => t('Link title'),
      '#text' => t('Link text'),
      '#url' => Url::fromUri('<front>'),
    ];
  }
}

Place your paragraph handler under src\Plugin\ParagraphHandler\ folder inside your module.

Roadmap

  • Admin UI to manage paragraph handlers. To enable and disable handlers.
Supporting organizations: 
D10 Development

Project information

Releases