Commit 63188cda authored by Hitesh Koli's avatar Hitesh Koli Committed by Hardik Patel
Browse files

Issue #3151734 by hitesh.koli, narendra.rajwar27: Notice: Undefined offset: 1...

Issue #3151734 by hitesh.koli, narendra.rajwar27: Notice: Undefined offset: 1 in page_specific_class_preprocess_html()
parent a05ba64e
Loading
Loading
Loading
Loading
+13 −8
Original line number Diff line number Diff line
@@ -2,9 +2,10 @@

/**
 * @file
 *
 * This file adds a class to the body tag page-wise.
 * 
 */

use Drupal\Core\Url;
use Drupal\Component\Utility\Html;
use Drupal\Core\Routing\RouteMatchInterface;
@@ -21,16 +22,16 @@ function page_specific_class_preprocess_html(&$variables) {

  // Get settings from page specific class settings.
  $config = \Drupal::config('page_specific_class.settings');
  if (!empty($config->get('url_with_class'))) {
    $enteredArr = explode(PHP_EOL, $config->get('url_with_class'));

  foreach ($enteredArr as $values) {
    foreach ($enteredArr as $key => $values) {
      $urlWithClassArr = explode("|", $values);
    $url = trim(strtolower($urlWithClassArr[0]));
    if (isset($urlWithClassArr[1])) {
      $url = trim($urlWithClassArr[0]);
      $class = trim($urlWithClassArr[1]);
      $classes_array = explode(' ', $class);
      $front_page = \Drupal::service('path.matcher')->isFrontPage();
      $enteredPath = \Drupal::service('path.alias_manager')->getPathByAlias(Html::escape($url));
      $enteredPath = \Drupal::service('path.alias_manager')->getPathByAlias($url);

      // If current path and entered path by user in page class setting match, 
      // then only add respective class.
@@ -39,9 +40,8 @@ function page_specific_class_preprocess_html(&$variables) {
          $variables['attributes']['class'][] = Html::cleanCssIdentifier($class, []);
        }
      }

      // If current page matches with home page or user entered path for all,
      // pages then only add respective class.
      // If current page matches with home page or user entered path for all pages,
      // then only add respective class.
      else if (($front_page) && ($enteredPath == "/<front>") || ($enteredPath == "/*")) {
        foreach ($classes_array as $class) {
          $variables['attributes']['class'][] = Html::cleanCssIdentifier($class, []);
@@ -62,6 +62,11 @@ function page_specific_class_help($route_name, RouteMatchInterface $route_match)
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t("Page Specific Class allows users to add classes to body of any page through the configuration interface. Hooray for more powerful page theming!") . '</p>';

      $output .= '<h3>' . t('Installation note') . '</h3>';
      $output .= '<dl>';
      $output .= '<dt>' . t('Enable the module on <a href=":extend_link">extend menu</a>.', [':extend_link' => Url::fromRoute('system.modules_list')->toString()]) . '</dt>';
      $output .= '</dl>';

      $output .= '<h3>' . t('Usage') . '</h3>';
      $output .= '<dl>';
      $output .= '<dt>' . t('To add a class to a body tag of page, simply visit that  configuration page at <a href=":configure_link">Configure</a>.', [':configure_link' => Url::fromRoute('page_specific_class.settings')->toString()]) . '</dt>';