You are here

ParagraphBlocksPanelsIpeBaseManager.php in Paragraph blocks 8

File

src/ParagraphBlocksPanelsIpeBaseManager.php
View source
<?php

namespace Drupal\paragraph_blocks;

use Drupal\Core\Form\FormStateInterface;

/**
 * Handles panels IPE hooks to rename paragraph blocks.
 */
class ParagraphBlocksPanelsIpeBaseManager {

  /**
   * The plugin type id.
   *
   * @var string
   */
  protected $pluginTypeId;

  /**
   * The label format.
   *
   * @var string
   */
  protected $labelFormat;

  /**
   * The current entity, or NULL.
   *
   * @var \Drupal\Core\Entity\EntityInterface|null
   */
  protected $entity;

  /**
   * PanelsParagraphsPanelsIpeBaseManager constructor.
   *
   * @param string $plugin_type_id
   *   The plugin type id.
   * @param string $label_format
   *   The label format.
   * @param \Drupal\Core\Entity\EntityInterface|null $entity
   *   The entity.
   */
  public function __construct($plugin_type_id, $label_format, $entity) {
    $this->pluginTypeId = $plugin_type_id;
    $this->labelFormat = $label_format;
    $this->entity = $entity;
  }

  /**
   * Change the title on the add/edit form.
   *
   * @param array &$form
   *   The form.
   * @param \Drupal\hcp_paragraphs\FormStateInterface $form_state
   *   The form state.
   *
   * @see hook_form_FORM_ID_alter()
   */
  public function hookFormPanelsIpeBlockPluginFormAlter(array &$form, FormStateInterface $form_state) {
    $title = $this
      ->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;
    }
  }

  /**
   * Removes unused paragraphs and update the panels title from the paragraph.
   *
   * @param array $blocks
   *   The blocks.
   *
   * @see hook_panels_ipe_blocks_alter()
   */
  public function hookPanelsIpeBlocksAlter(array &$blocks) {

    // Loop through all of the blocks.
    foreach ($blocks as $delta => $block) {
      $title = $this
        ->getTitle($block['plugin_id']);
      if ($title === FALSE) {

        // Remove the block if there is no paragraph data for the delta.
        unset($blocks[$delta]);
      }
      elseif ($title !== NULL) {

        // Replace the title.
        $blocks[$delta]['label'] = $title;
      }
    }
  }

  /**
   * Returns the plugin's paragraph title.
   *
   * @param string $plugin_id
   *   The plugin id.
   *
   * @return string
   *   The paragraph title.
   */
  protected function getTitle($plugin_id) {
    $plugin_parts = explode(':', $plugin_id);
    if (count($plugin_parts) < 4) {
      return NULL;
    }
    list($plugin_type_id, , $plugin_field_name, $plugin_field_delta) = $plugin_parts;
    if ($plugin_type_id != $this->pluginTypeId) {
      return NULL;
    }

    // Return if this plugin should be removed from the list.
    if (!$this->entity || $plugin_field_delta >= $this->entity
      ->get($plugin_field_name)
      ->count()) {
      return FALSE;
    }

    // Get the referenced paragraph.

    /** @var \Drupal\paragraphs\Entity\Paragraph $paragraph */
    $paragraph = $this->entity
      ->get($plugin_field_name)
      ->referencedEntities()[$plugin_field_delta];

    // Change the label to match admin_label from the referenced paragraph.
    return str_replace('@label', $paragraph
      ->getSummary(), $this->labelFormat);
  }

}

Classes

Namesort descending Description
ParagraphBlocksPanelsIpeBaseManager Handles panels IPE hooks to rename paragraph blocks.