You are here

block_content_machine_name.module in Block Content Machine Name 8.2

Same filename and directory in other branches
  1. 8 block_content_machine_name.module

Module file for block_content_machine_name.

File

block_content_machine_name.module
View source
<?php

/**
 * @file
 * Module file for block_content_machine_name.
 */
use Drupal\Component\Utility\Html;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\EntityTypeInterface;

/**
 * Implements hook_entity_base_field_info().
 */
function block_content_machine_name_entity_base_field_info(EntityTypeInterface $entity_type) {
  if ($entity_type
    ->id() == 'block_content') {
    $fields['machine_name'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Machine name'))
      ->setDescription(t('Machine name of the block'))
      ->setRequired(TRUE)
      ->setSetting('max_length', 255)
      ->addConstraint('UniqueField', [])
      ->setDisplayOptions('form', [
      'type' => 'machine_name',
      'weight' => -4,
      'settings' => [
        'source' => [
          'info',
          'widget',
          0,
          'value',
        ],
        'exists' => 'block_content_machine_name_existing_block_content_name',
      ],
    ])
      ->setDisplayConfigurable('form', TRUE);
    $fields['template_suggestion'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Template suggestion'))
      ->setDescription(t('template suggestion for this block content. i.e. block__test'))
      ->setRequired(FALSE)
      ->setSetting('max_length', 255)
      ->setDisplayOptions('form', [
      'type' => 'string_textfield',
      'weight' => -5,
    ])
      ->setDisplayConfigurable('form', TRUE)
      ->setTranslatable(FALSE);
    return $fields;
  }
}

/**
 * Check the given machine name already exists or not.
 *
 * @param string $id
 *   The machine name.
 *
 * @return bool
 *   Return true if machine_name exists else false.
 */
function block_content_machine_name_existing_block_content_name($id) {
  if (\Drupal::entityTypeManager()
    ->getStorage('block_content')
    ->getQuery()
    ->condition('machine_name', $id)
    ->range(0, 1)
    ->count()
    ->execute()) {
    return TRUE;
  }
  return FALSE;
}

/**
 * Implements hook_preprocess_HOOK() for block templates.
 */
function block_content_machine_name_preprocess_block(&$variables) {
  switch ($variables['base_plugin_id']) {
    case 'block_content':
      $block_content = $variables['content']['#block_content'];
      $machine_name = $block_content
        ->get('machine_name')->value;
      $cssclass = 'block-content--' . strtolower($machine_name);
      $variables['attributes']['class'][] = Html::cleanCssIdentifier($cssclass);
      $variables['attributes']['class'][] = 'block-type-block-content';
      break;
  }
}

/**
 * Implements hook_theme_suggestions_block_alter().
 */
function block_content_machine_name_theme_suggestions_block_alter(array &$suggestions, array $variables) {

  // Add template suggestion based on machine name.
  switch ($variables['elements']['#base_plugin_id']) {
    case 'block_content':
      $block_content = $variables['elements']['content']['#block_content'];
      $machine_name = $block_content
        ->get('machine_name')->value;
      $suggestions[] = 'block__' . $block_content
        ->getEntityTypeId() . '__' . $machine_name;
      $suggestions[] = 'block__' . $block_content
        ->getEntityTypeId() . '__' . $block_content
        ->bundle() . '__' . $machine_name;
      $template_suggestion = $block_content
        ->get('template_suggestion')->value;
      if (!empty($template_suggestion)) {
        $suggestions[] = $template_suggestion;
      }
      break;
  }
}

Functions

Namesort descending Description
block_content_machine_name_entity_base_field_info Implements hook_entity_base_field_info().
block_content_machine_name_existing_block_content_name Check the given machine name already exists or not.
block_content_machine_name_preprocess_block Implements hook_preprocess_HOOK() for block templates.
block_content_machine_name_theme_suggestions_block_alter Implements hook_theme_suggestions_block_alter().