You are here

fixed_block_content.module in Fixed Block Content 8

Provides permanent custom content blocks.

File

fixed_block_content.module
View source
<?php

/**
 * @file
 * Provides permanent custom content blocks.
 */
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\fixed_block_content\Entity\FixedBlockContent;
use Drupal\Core\Entity\EntityFormInterface;
use Drupal\block_content\BlockContentInterface;

/**
 * @file
 * Fixed Block Content module.
 *
 * Provides fixed placements for custom blocks.
 */

/**
 * {@inheritdoc}
 */
function fixed_block_content_block_content_delete(EntityInterface $entity) {

  /** @var \Drupal\fixed_block_content\FixedToContentMappingHandlerInterface $mapping_handler */
  $mapping_handler = \Drupal::entityTypeManager()
    ->getHandler('fixed_block_content', 'mapping_handler');

  // Search the linked fixed block for the deleted block content.
  if ($fixed_block = $mapping_handler
    ->getFixedBlock($entity)) {

    // Break the link between both.
    $mapping_handler
      ->releaseBlockContent($fixed_block);
  }
}

/**
 * Implements hook_form_alter().
 */
function fixed_block_content_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  // Add an option to the custom block form to update the default content in
  // its fixed block.
  if (preg_match('/^block_content_.+_edit_form$/', $form_id) && \Drupal::currentUser()
    ->hasPermission('administer blocks') && ($form_object = $form_state
    ->getFormObject()) && $form_object instanceof EntityFormInterface && ($block_content = $form_object
    ->getEntity()) && $block_content instanceof BlockContentInterface) {

    /** @var \Drupal\fixed_block_content\FixedToContentMappingHandlerInterface $mapping_handler */
    $mapping_handler = \Drupal::entityTypeManager()
      ->getHandler('fixed_block_content', 'mapping_handler');

    // Search the fixed block of the edited custom block.
    if ($fixed_block = $mapping_handler
      ->getFixedBlock($block_content)) {
      $form['fixed_block_content_update'] = [
        '#type' => 'checkbox',
        '#title' => t('Update default content in the fixed block'),
        '#default_value' => FALSE,
        '#weight' => 99,
      ];
      array_unshift($form['actions']['submit']['#submit'], '_fixed_block_content_update_on_save');
      $form_state
        ->setTemporaryValue('fixed_block_content_fbid', $fixed_block
        ->id());
    }
  }
}

/**
 * Block content form submit handler. Updates default content in fixed block.
 *
 * @param $form
 *   The submitted form.
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 *   The form state.
 */
function _fixed_block_content_update_on_save(&$form, FormStateInterface $form_state) {
  if ($form_state
    ->getValue('fixed_block_content_update') && ($fbid = $form_state
    ->getTemporaryValue('fixed_block_content_fbid')) && ($fixed_block = FixedBlockContent::load($fbid))) {

    // The operation itself is done on the custom block update hook.
    // @see fixed_block_content_block_content_update().
    $fbc_to_update =& drupal_static('fixed_block_content_update', []);
    $fbc_to_update[$form_state
      ->getFormObject()
      ->getEntity()
      ->id()] = $fixed_block;
  }
}

/**
 * {@inheritdoc}
 */
function fixed_block_content_block_content_update(EntityInterface $entity) {

  // Check for any pending update of fixed block default content.
  $fbc_to_update =& drupal_static('fixed_block_content_update', []);
  $block_id = $entity
    ->id();
  if (isset($fbc_to_update[$block_id])) {
    $fbc_to_update[$block_id]
      ->importDefaultContent();
    $fbc_to_update[$block_id]
      ->save();
    unset($fbc_to_update[$block_id]);
  }
}

Functions