You are here

paragraph_blocks.module in Paragraph blocks 8

Same filename and directory in other branches
  1. 8.2 paragraph_blocks.module
  2. 3.x paragraph_blocks.module

File

paragraph_blocks.module
View source
<?php

/**
 * @file
 * Contains paragraph_blocks.module.
 */
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\paragraph_blocks\Entity\ParagraphBlocksEntity;
use Drupal\paragraph_blocks\ParagraphBlocksPanelsIpeManager;
use Drupal\paragraph_blocks\Plugin\Field\FieldWidget\ParagraphBlocksInlineParagraphsWidget;

/**
 * Implements hook_help().
 */
function paragraph_blocks_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help for the paragraph_blocks module.
    case 'help.page.paragraph_blocks':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Panels paragraphs') . '</p>';
      return $output;
    default:
  }
}

/**
 * Implements hook_entity_base_field_info().
 */
function paragraph_blocks_entity_base_field_info(EntityTypeInterface $entity_type) {
  if ($entity_type
    ->id() == 'paragraph') {
    $fields = [];
    $fields['admin_title'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Admin title'))
      ->setDescription(t('The admin title is used to help place paragraphs into panes.'))
      ->setTranslatable(TRUE)
      ->setDisplayOptions('form', [
      'type' => 'string_textfield',
      'weight' => -10,
    ])
      ->setDisplayConfigurable('form', TRUE);
    return $fields;
  }
}

/**
 * Implements hook_panels_ipe_blocks_alter().
 */
function paragraph_blocks_panels_ipe_blocks_alter(&$blocks) {

  // Remove unused paragraphs and update the panels title from the paragraph.
  $panels_ipe_manager = ParagraphBlocksPanelsIpeManager::create(\Drupal::getContainer());
  $panels_ipe_manager
    ->hookPanelsIpeBlocksAlter($blocks);

  // Change the title of the "Content" block to "Full content".
  foreach ($blocks as $delta => $block) {
    if ($block['plugin_id'] == 'entity_field:node:field_content') {
      $blocks[$delta]['label'] = t('Full content');
      break;
    }
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function paragraph_blocks_form_panels_ipe_block_plugin_form_alter(&$form, FormStateInterface $form_state) {

  // Get the paragraph blocks entity manager.

  /** @var \Drupal\paragraph_blocks\ParagraphBlocksEntityManager $paragraph_blocks_entity_manager */
  $paragraph_blocks_entity_manager = \Drupal::service('paragraph_blocks.entity_manager');

  // Get the title of the plugin.
  $title = $paragraph_blocks_entity_manager
    ->getTitle($form['plugin_id']['#value']);
  if (!empty($title)) {
    $section =& $form['flipper']['front']['settings'];
    $section['admin_label']['#type'] = 'hidden';
    $section['label']['#type'] = 'hidden';
    $section['label']['#required'] = FALSE;
    $section['label_display']['#type'] = 'hidden';
    $section['label_display']['#value'] = 0;
    $section['label']['#value'] = $title;
  }
}

/**
 * Implements hook_widget_info_alter().
 */
function paragraph_blocks_field_widget_info_alter(&$info) {
  $info['entity_reference_paragraphs']['class'] = ParagraphBlocksInlineParagraphsWidget::class;
}

/**
 * Implements hook_entity_type_alter().
 */
function paragraph_blocks_entity_type_alter(&$entity_types) {

  // @todo: Allow multiple modules to override this class (with reflection)?

  /** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */
  $entity_types['paragraph']
    ->setClass(ParagraphBlocksEntity::class);
}