You are here

block_id.module in Block ID 2.0.x

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

Contains block_id.module.

File

block_id.module
View source
<?php

/**
 * @file
 * Contains block_id.module.
 */
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\block\Entity\Block;
use Drupal\block\BlockInterface;

/**
 * Implements hook_help().
 */
function block_id_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help for the block_id module.
    case 'help.page.block_id':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Adds an additional field to set the ID of a block.') . '</p>';
      return $output;
    default:
  }
}

/**
 * Implements hook_ENTITY_TYPE_presave().
 */
function block_id_block_presave(BlockInterface $entity) {
  if (empty($entity
    ->getThirdPartySetting('block_id', 'id'))) {
    $entity
      ->unsetThirdPartySetting('block_id', 'id');
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function block_id_form_block_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if (\Drupal::currentUser()
    ->hasPermission('administer block id')) {
    $block = $form_state
      ->getFormObject()
      ->getEntity();
    $form['third_party_settings']['#tree'] = TRUE;
    $form['third_party_settings']['block_id']['id'] = [
      '#type' => 'textfield',
      '#title' => t('Block ID'),
      '#description' => t('CSS ID. Separate multiple id by spaces. (eg. id_1 id_2)'),
      '#default_value' => $block
        ->getThirdPartySetting('block_id', 'id'),
    ];

    // checking w3 standards validation.
    $form['#validate'][] = 'W3S_validation';
  }
}

/**
 * Implement w3 standards validation alter.
*/
function W3S_validation(&$form, FormStateInterface $form_state) {

  // Block ID field value fatch.
  $inputs = $form_state
    ->getUserInput()['third_party_settings']['block_id']['id'];
  if (!empty($inputs)) {

    // Checking id has not no space and special characters.
    if (preg_match('/[^a-zA-Z_\\-0-9]/i', $inputs)) {
      $form_state
        ->setErrorByName('third_party_settings', t('Attribute ID must be unique, must not contain any space characters & must contain at least one character. Underscore (_) can be used.'));
    }

    // block list those using block id field.
    $block_ids = \Drupal::entityQuery('block')
      ->condition('third_party_settings', '')
      ->execute();

    // Getting ID's already inserted in another block.
    foreach ($block_ids as $ids) {
      $block = Block::load($ids);
      if ($block
        ->getThirdPartySetting('block_id', 'id') == $inputs) {
        $form_state
          ->setErrorByName('third_party_settings', t('Attribute ID must be unique. This ID has added in another block.'));
      }
    }
  }
}

/**
 * Implements hook_preprocess_HOOK().
 */
function block_id_preprocess_block(&$variables) {
  if (!empty($variables['elements']['#id'])) {
    $block = Block::load($variables['elements']['#id']);
    if ($block && ($ids = $block
      ->getThirdPartySetting('block_id', 'id'))) {
      $id_array = explode(' ', $ids);
      $variables['attributes']['id'] = $id_array;
    }
  }
}

Functions