You are here

vertical_tabs_config.module in Vertical Tabs Config 8

Same filename and directory in other branches
  1. 7 vertical_tabs_config.module

Vertical tabs config main file.

File

vertical_tabs_config.module
View source
<?php

/**
 * @file
 * Vertical tabs config main file.
 */
use Drupal\Core\Database\Database;
use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 *
 * Hide vertical tabs according to configuration.
 */
function vertical_tabs_config_form_node_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $user = \Drupal::currentUser();
  $roles = $user
    ->getRoles();

  // 1. Vertical tabs visibility.
  $config = vertical_tabs_config_get_config();
  $current_path = \Drupal::service('path.current')
    ->getPath();
  $path = explode('/', $current_path);
  if ($path[1] == 'node' && ($path[2] == 'add' || $path[3] == 'edit')) {

    // Maybe in $form there is the content_type like in D7.
    $content_type = str_replace('node_', '', $form_id);
    $content_type = str_replace('_edit_form', '', $content_type);
    $content_type = str_replace('_form', '', $content_type);
    if (!empty($config[$content_type])) {

      // If restricted roles array is empty restrictions apply to all roles.
      if (empty($config[$content_type]['roles']) || vertical_tabs_config_user_is_restricted_by_all_his_roles($config[$content_type]['roles'])) {
        foreach ($config[$content_type] as $vt_machine_name => $hidden) {
          if ($hidden == "1") {
            hide($form[$vt_machine_name]);
          }
        }
      }
      else {

        // Vertical tabs restriction for specific roles.
        foreach ($roles as $rid_user) {
          foreach ($config[$content_type]['roles'] as $rid_restricted) {
            if ($rid_user == $rid_restricted) {
              foreach ($config[$content_type] as $vt_machine_name => $hidden) {
                if ($hidden == "1") {
                  hide($form[$vt_machine_name]);
                }
              }
            }
          }
        }
      }
    }
  }

  // 2. Vertical tabs order.
  $order = 0;
  $vertical_tabs = vertical_tabs_config_vertical_tab_list();
  foreach ($vertical_tabs as $vt_machine_name => $vt_human_name) {

    // Important note: metatag vertical tab cannot be moved, it always appears
    // on top if its weight is changed.
    // Other vertical tabs position can't also be moved.
    $excluded = [
      'metatag',
    ];
    if (isset($form[$vt_machine_name]) && !in_array($vt_machine_name, $excluded)) {
      $vt_weight = \Drupal::config('vertical_tabs_config.order')
        ->get('vertical_tabs_config_' . $vt_machine_name);
      $form[$vt_machine_name]['#weight'] = $vt_weight;
      $order++;
    }
  }
}

/**
 * Checks if user is restricted by configuration given all of his roles.
 *
 * @param array $restricted_roles
 *   List of all restricted roles.
 *
 * @return bool
 *   Indicates if user is restricted given all his roles.
 */
function vertical_tabs_config_user_is_restricted_by_all_his_roles(array $restricted_roles) {
  $user = \Drupal::currentUser();
  $roles = $user
    ->getRoles();
  $restricted_counter = 0;
  foreach ($roles as $user_role) {
    foreach ($restricted_roles as $role) {
      if ($user_role == $role) {
        $restricted_counter++;
      }
    }
  }
  if ($restricted_counter == count($roles)) {
    return TRUE;
  }
  else {
    return FALSE;
  }
}

/**
 * Returns a list of all vertical tabs.
 *
 * @param bool $ordered_by_weight Return a list ordered by weight?
 *
 * @return array List of vertical tabs
 */
function vertical_tabs_config_vertical_tab_list($ordered_by_weight = FALSE) {
  $config = \Drupal::config('vertical_tabs_config.order');
  $vertical_tabs = [
    'options' => t('Promotion options'),
    'menu' => t('Menu settings'),
    'revision_information' => t('Revision information'),
    'path_settings' => t('URL path settings'),
    'author' => t('Authoring information'),
    'book' => t('Book outline'),
    'ds_switch_view_mode' => t('Display settings'),
  ];
  if ($ordered_by_weight) {
    $unordered = [];
    foreach ($vertical_tabs as $vt_machine_name => $vt_human_name) {
      $weight = $config
        ->get('vertical_tabs_config_' . $vt_machine_name);
      $unordered[$weight] = [
        'key' => $vt_machine_name,
        'value' => $vt_human_name,
      ];
    }
    ksort($unordered);
    $vertical_tabs = [];
    foreach ($unordered as $vt_assoc) {
      $vertical_tabs[$vt_assoc['key']] = $vt_assoc['value'];
    }
  }
  return $vertical_tabs;
}

/**
 * Returns module visibility configuration or -1.
 */
function vertical_tabs_config_get_config() {
  try {
    $query = Database::getConnection()
      ->select('vertical_tabs_config', 'n');
    $query
      ->fields('n', [
      'id',
      'vertical_tab',
      'content_type',
      'roles',
      'hidden',
    ]);
    $result = $query
      ->execute()
      ->fetchAll();
  } catch (Exception $e) {
    \Drupal::logger('vertical_tabs_config')
      ->notice($e
      ->getMessage());
    return "-1";
  }
  if (isset($result) && is_array($result)) {
    $conf_hash = [];
    foreach ($result as $obj) {
      $conf_hash[$obj->content_type][$obj->vertical_tab] = $obj->hidden;
      if (!isset($conf_hash[$obj->content_type]['roles'])) {
        $conf_hash[$obj->content_type]['roles'] = json_decode($obj->roles);
      }
    }
    return $conf_hash;
  }
  else {
    return "-1";
  }
}

Functions

Namesort descending Description
vertical_tabs_config_form_node_form_alter Implements hook_form_BASE_FORM_ID_alter().
vertical_tabs_config_get_config Returns module visibility configuration or -1.
vertical_tabs_config_user_is_restricted_by_all_his_roles Checks if user is restricted by configuration given all of his roles.
vertical_tabs_config_vertical_tab_list Returns a list of all vertical tabs.