PHP Classes

File: icon_block.module

Recommend this page to a friend!
  Classes of Malik Naik   Drupal Icon Module   icon_block.module   Download  
File: icon_block.module
Role: Example script
Content type: text/plain
Description: Example script
Class: Drupal Icon Module
Add icons to a block of content managed by Drupal
Author: By
Last change:
Date: 4 years ago
Size: 8,428 bytes
 

Contents

Class file image Download
<?php

/**
 * @file
 * Drupal integration of icon to blocks
 */

use Drupal\Core\Form\FormStateInterface;
use
Symfony\Component\Yaml\Yaml;

/**
 * Implements hook_preprocess_HOOK()
 */
/*
function icon_block_preprocess_block(&$variables) {
  $settings ='block.block.' . $variables['elements']['#id'];
  $config = icon_block_get_config_factory($settings);

  if($config->get('icon')) {
  }
}
*/
/**
 * Get the editable configuration factory
 */

function icon_block_get_config_factory($settings) {
 
$config = \Drupal::config('icon_block.settings');
 
//$config = \Drupal::configFactory()->get($settings);
 
return $config;
}


/**
 * Implements hook_preprocess_HOOK()
 */

function icon_block_preprocess_block(&$variables) {
 
/*$config = \Drupal::configFactory();
  $config = $config->getEditable('block.block.' . $variables['elements']['#id']);*/

 
$tags = 'icon_block.' . $variables['elements']['#id'] . '.';
 
$config = \Drupal::config('icon_block.settings');

 
//$variables['title_prefix']['#markup'] = t('<h1>' . . '</h1>');

 
if($config->get($tags . 'icon')) {
   
$icon = $config->get($tags . 'icon');
   
$tag = $config->get($tags . 'wrapper');
   
$wrapper_classes = explode(',', $config->get($tags . 'wrapper_class'));
   
$classes = '';
    foreach(
$wrapper_classes as $c) {
     
$classes .= trim($c) . ' ';
    }

   
//$variables['title_prefix']['#markup'] = 'hello_' . $tag . 'position' . '_world';
   
switch(trim($config->get($tags . 'position'))) {
      case
'title_before':
       
$variables['title_prefix']['#markup'] = t(_icon_block_get_icon_tag($tag, $icon, $classes));
      break;
      case
'title_after':
       
$variables['title_suffix']['#markup'] = t(_icon_block_get_icon_tag($tag, $icon, $classes));
      break;
      case
'title_inside_before':
       
$variables['label'] = t(_icon_block_get_icon_tag($tag, $icon, $classes) . $variables['label']);
      break;
      case
'title_inside_after':
       
$variables['label'] = t($variables['label'] . _icon_block_get_icon_tag($tag, $icon, $classes));
      break;
      case
'content_before':
        if(
array_key_exists('#items', $variables['content'])) {
         
$arr = _icon_block_get_icon_list_tag($tag, $icon, $classes);
         
$variables['content']['#items'] = $arr + $variables['content']['#items'];
        }else{
         
$markup = array_key_exists('#markup', $variables['content']) ? $variables['content']['#markup'] : '';

         
$variables['content']['#markup'] = t('<div class="icon-div-block">' . _icon_block_get_icon_tag($tag, $icon, $classes) . '</div>') . $markup;
        }
      break;
      case
'content_after':
        if(
array_key_exists('#items', $variables['content'])) {
         
$arr = _icon_block_get_icon_list_tag($tag, $icon, $classes);
         
$variables['content']['#items'] += $arr;
         
//$variables['content']['#items'] + $arr;
       
}else{
         
$markup = array_key_exists('#markup', $variables['content']) ? $variables['content']['#markup'] : '';

         
$variables['content']['#markup'] = $markup . t('<div class="icon-div-block">' . _icon_block_get_icon_tag($tag, $icon, $classes) . '</div>');
        }
      break;
      default:
       
// Do Nothing
   
}
  }
}

/**
 * Get the icon tag
 */

function _icon_block_get_icon_tag($tag, $icon, $classes) {
  if(
$tag != 'i' && $tag != 'span') {
   
$tag = 'i';
  }
 
$markup = '<' . $tag . ' class="fa fa-' . $icon . ' ' . $classes . '"></' . $tag . '>';
  return
$markup;
}

/**
 * Get the icon list tag
 */

function _icon_block_get_icon_list_tag($tag, $icon, $classes) {
  if(
$tag != 'i' || $tag != 'span') {
   
$tag = 'i';
  }

 
$arr['icon.list'] = array(
   
'is_expanded' => FALSE,
   
'is_collapsed' => FALSE,
   
'in_active_trail' => FALSE,
   
'title' => t('<div class="icon-div-block"><:tag class="fa fa-:icon :class"></:tag></div>', [
     
':tag' => $tag,
     
':icon' => $icon,
     
':class' => $classes,
    ]),
   
'url' => \Drupal\Core\Url::fromUri('http://drupal.org/'),
   
'attributes' => new \Drupal\Core\Template\Attribute(array(
     
'class' => array('icon_block_icon-link', 'menu-item'),
    )),
  );

  return
$arr;
}

/**
 * Implements hook_form_FORM_ID_alter()
 */

function icon_block_form_block_form_alter(array &$form, FormStateInterface &$form_state, $form_id) {
 
$settings = _icon_block_get_settings_name($form['#cache']['tags']);
 
// str_replace('config:', '', $form['#cache']['tags'][0]);
  //$config = \Drupal::configFactory()->getEditable($settings);

  //$config = \Drupal::config('icon_block.settings');
 
$config = icon_block_get_config_factory($settings);
 
$tag = 'icon_block.' . $settings . '.';

 
$form['settings']['icon_selector'] = array(
   
'#type' => 'details',
   
'#tree' => TRUE,
   
'#title' => t('Icon'),
   
'#open' => TRUE,
   
'#collapsible' => TRUE,
   
'#collapsed' => FALSE
 
);

 
$form['settings']['icon_selector']['icon'] = array(
   
'#type' => 'textfield',
   
'#title' => t('Select Icon'),
   
'#field_prefix' => 'fa-',
   
'#default_value' => $config->get($tag . 'icon') ? $config->get($tag . 'icon') : '',
   
'#autocomplete_route_name' => 'icon_block.autocomplete',
  );

 
$form['settings']['icon_selector']['wrapper'] = array(
   
'#type' => 'select',
   
'#title' => t('Icon Wrapper'),
   
'#description' => t('Choose an HTML element to wrap the icon with.'),
   
'#default_value' => $config->get($tag . 'wrapper') ? $config->get($tag . 'wrapper') : 'i',
   
'#options' => array(
     
'i' => t('i'),
     
'span' => t('span'),
    ),
   
'#states' => array(
     
'invisible' => array(
       
':input[name="settings[icon_selector][icon]"]' => array(
         
'value' => ''
       
),
      )
    ),
  );

 
$form['settings']['icon_selector']['wrapper_class'] = array(
   
'#type' => 'textfield',
   
'#title' => t('Icon Wrapper Classes'),
   
'#description' => t('A space separated list of CSS classes.'),
   
'#default_value' => $config->get($tag . 'wrapper_class') ? $config->get($tag . 'wrapper_class') : '',
   
'#states' => array(
     
'invisible' => array(
        array(
':input[name="settings[icon_selector][wrapper]"]' => array(
         
'value' => 'None'
       
)),
       
'and',
        array(
':input[name="settings[icon_selector][icon]"]' => array(
         
'value' => ''
       
)),
      )
    ),
  );

 
$form['settings']['icon_selector']['position'] = array(
   
'#type' => 'select',
   
'#title' => t('Position'),
   
'#default_value' => $config->get($tag . 'position') ? $config->get($tag . 'position') : 'title_before',
   
'#options' => array(
         
'title_before' => t('Before title'),
         
'title_after' => t('After title'),
         
'title_inside_before' => t('Before title (inside markup)'),
         
'title_inside_after' => t('After title (inside markup)'),
         
'content_before' => t('Before content'),
         
'content_after' => t('After content'),
        ),
   
'#states' => array(
     
'invisible' => array(
       
':input[name="settings[icon_selector][icon]"]' => array(
         
'value' => ''
       
),
      )
    ),
  );

 
$form['actions']['submit']['#submit'][] = '_icon_block_submit_value';

 
//ksm($form);
}


/**
 * Return the blocks settings name
 */

function _icon_block_get_settings_name($names) {
  if(!
is_array($names)) {
    foreach(
$names as $name) {
      if(
substr($name, 0, 7) == 'config:') {
        return
str_replace('.', '', substr($name, 19));
      }
    }
  }else{
    return
str_replace('.', '', substr($name, 19));
  }
}

/**
 * Stores the icon information
 */
function _icon_block_submit_value(array $form, FormStateInterface $form_state) {

 
$config = \Drupal::configFactory()->getEditable('icon_block.settings');
 
$settings = _icon_block_get_settings_name($form['#cache']['tags']);

 
//$config->delete();

 
$values = $form_state->getValue('settings')['icon_selector'];
 
$tag = 'icon_block.' . $settings . '.';

 
$config->set($tag . 'icon', $values['icon'])
    ->
set($tag . 'wrapper', $values['wrapper'])
    ->
set($tag . 'wrapper_class', $values['wrapper_class'])
    ->
set($tag . 'position', $values['position'])
    ->
save();
}

/**
 * Implements hook_page_attachments()
 */
function icon_block_page_attachments(array &$page) {
 
// Adding CSS
 
$page['#attached']['library'][] = 'icon_block/custom_css';

 
// Adding JS
 
$page['#attached']['library'][] = 'icon_block/custom_js';
 
$page['#attached']['library'][] = 'icon_block/fontawesome_js';
}