You are here

social_content_block.module in Open Social 8.9

The Social Content Block module.

File

modules/social_features/social_content_block/social_content_block.module
View source
<?php

/**
 * @file
 * The Social Content Block module.
 */
use Drupal\block_content\BlockContentInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\social_content_block\ContentBuilder;

/**
 * Implements hook_preprocess_block().
 */
function social_content_block_preprocess_block(&$variables) {
  if (isset($variables['content']['#block_content']) && $variables['content']['#block_content']
    ->bundle() === 'custom_content_list') {
    if ($variables['base_plugin_id'] === 'block_content') {
      $variables['card'] = TRUE;
    }
    elseif ($variables['base_plugin_id'] === 'inline_block') {
      $block_entity = $variables['content']['#block_content'];
      if (!$block_entity instanceof BlockContentInterface || $block_entity
        ->bundle() !== 'custom_content_list') {
        return;
      }
      $variables['content_type'] = [
        '#plain_text' => _social_content_block_get_content_type_for_block($block_entity),
      ];
      if (!$block_entity->field_subtitle
        ->isEmpty()) {
        $variables['subtitle'] = $block_entity->field_subtitle
          ->view([
          'label' => 'hidden',
        ]);
      }
    }
  }
}

/**
 * Implements hook_entity_extra_field_info().
 */
function social_content_block_entity_extra_field_info() {
  return [
    'block_content' => [
      'custom_content_list' => [
        'display' => [
          'entities' => [
            'label' => t('Entities'),
            'weight' => 0,
          ],
        ],
      ],
    ],
  ];
}

/**
 * Implements hook_ENTITY_TYPE_view().
 */
function social_content_block_block_content_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
  if ($entity
    ->bundle() === 'custom_content_list' && $display
    ->getComponent('entities')) {
    $build['social_content_block'] = [
      '#lazy_builder' => [
        'social_content_block.content_builder:build',
        [
          $entity
            ->id(),
          $entity
            ->getEntityTypeId(),
          $entity
            ->bundle(),
        ],
      ],
      '#create_placeholder' => TRUE,
    ];
  }
}

/**
 * Get content type for the block.
 *
 * @param \Drupal\block_content\BlockContentInterface $block_entity
 *   The block content entity object.
 *
 * @return string
 *   The entity type.
 */
function _social_content_block_get_content_type_for_block(BlockContentInterface $block_entity) {
  if ($block_entity->field_plugin_id
    ->isEmpty()) {
    return '';
  }
  $plugin_id = $block_entity->field_plugin_id->value;
  $definition = \Drupal::service('plugin.manager.content_block')
    ->getDefinition($plugin_id);

  // For nodes we distinguish bundles, otherwise we only show the entity type.
  if ($definition['entityTypeId'] === 'node') {
    return $definition['bundle'];
  }
  return $definition['entityTypeId'];
}

/**
 * Implements hook_form_alter().
 */
function social_content_block_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  switch ($form_id) {
    case 'block_content_custom_content_list_form':
    case 'block_content_custom_content_list_edit_form':
      $element =& $form;
      break;
    case 'layout_builder_add_block':

      /** @var \Drupal\layout_builder\SectionComponent $component */
      $component = $form_state
        ->get('layout_builder__component');
      if ($component
        ->getPluginId() === 'inline_block:custom_content_list') {
        $element =& $form['settings']['block_form'];
      }
      break;
    case 'layout_builder_update_block':
      if (isset($form['settings']['block_form']['#block']) && $form['settings']['block_form']['#block']
        ->bundle() === 'custom_content_list') {
        $element =& $form['settings']['block_form'];
      }
      break;
  }
  if (isset($element)) {
    $element['#process'][] = [
      ContentBuilder::class,
      'processBlockForm',
    ];
  }
}

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 */
function social_content_block_form_block_content_custom_content_list_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  // Add submit handler to clear cache.
  foreach (array_keys($form['actions']) as $action) {
    if ($action !== 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
      $form['actions'][$action]['#submit'][] = 'custom_content_block_form_submit';
    }
  }
}

/**
 * Custom submit handler.
 */
function custom_content_block_form_submit($form, FormStateInterface $form_state) {

  // Clear plugin cache.
  \Drupal::service('plugin.cache_clearer')
    ->clearCachedDefinitions();
}