You are here

paragraph_blocks.module in Paragraph blocks 8.2

Same filename and directory in other branches
  1. 8 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\EntityFormInterface;
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\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.

  /** @var \Drupal\paragraph_blocks\ParagraphBlocksLabeller $labeller */
  $labeller = \Drupal::service('paragraph_blocks.labeller');
  $labeller
    ->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\ParagraphBlocksLabeller $labeller */
  $labeller = \Drupal::service('paragraph_blocks.labeller');

  // Get the title of the plugin.
  $title = $labeller
    ->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_plugin_filter_block__layout_builder_alter().
 */
function paragraph_blocks_plugin_filter_block__layout_builder_alter(array &$definitions, array $extra) {

  // Remove unused paragraphs and update the panels title from the paragraph.

  /** @var \Drupal\paragraph_blocks\ParagraphBlocksLabeller $labeller */
  $labeller = \Drupal::service('paragraph_blocks.labeller');
  $labeller
    ->hookLayoutBuilderChooseBlocksAlter($definitions);
}

/**
 * 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);
}

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

  // Add a checkbox to field config form to enable/disable paragraph blocks
  // for a paragraph field.

  /** @var \Drupal\Core\Field\FieldConfigInterface $field_config */
  $field_config = $form_state
    ->getFormObject()
    ->getEntity();
  $settings = $field_config
    ->getSettings();
  if (isset($settings['handler']) && $settings['handler'] == 'default:paragraph') {
    $form['third_party_settings']['paragraph_blocks']['status'] = [
      '#type' => 'checkbox',
      '#title' => t('Enable Paragraph Blocks'),
      '#default_value' => $field_config
        ->getThirdPartySetting('paragraph_blocks', 'status', TRUE),
    ];
  }
}

/**
 * Implements hook_field_widget_WIDGET_TYPE_form_alter().
 */
function paragraph_blocks_field_widget_paragraphs_form_alter(&$element, FormStateInterface $form_state, $context) {
  _paragraph_blocks_process_widget_form($element, $form_state, $context);
}

/**
 * Implements hook_field_widget_WIDGET_TYPE_form_alter().
 */
function paragraph_blocks_field_widget_entity_reference_paragraphs_form_alter(&$element, FormStateInterface $form_state, $context) {
  _paragraph_blocks_process_widget_form($element, $form_state, $context);
}

/**
 * Common helper hides the admin title if it's not enabled on the field.
 *
 * @param array $element
 *   The field widget form element as constructed by
 *   \Drupal\Core\Field\WidgetBaseInterface::form().
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 *   The current state of the form.
 * @param array $context
 *   An associative array containing the key-value pairs.
 *
 * @see hook_field_widget_WIDGET_TYPE_form_alter()
 */
function _paragraph_blocks_process_widget_form(array &$element, FormStateInterface $form_state, array $context) {
  $form_object = $form_state
    ->getFormObject();
  if ($form_object instanceof EntityFormInterface) {
    $bundle = $form_object
      ->getEntity()
      ->bundle();

    /** @var \Drupal\Core\Field\FieldConfigInterface $field_config */
    $field_config = $context['items']
      ->getFieldDefinition()
      ->getConfig($bundle);
    if (!$field_config
      ->getThirdPartySetting('paragraph_blocks', 'status', TRUE)) {
      $element['subform']['admin_title']['#access'] = FALSE;
    }
  }
}