0

I have an URL textarea and I want to add a body class if the current URL is one of those that the user has input in the textarea.

This is my current code:

function MYTHEME_form_system_theme_settings_alter(&$form, &$form_state, $form_id = NULL) {
  $form['head_footer_hiding_urls'] = [
    '#type' => 'fieldset',
    '#title' => t('Hide header and footer on specific url\'s'),
    '#description' => t('Specific url\'s to hide header and footer'),
    '#collabsible' => TRUE,
    '#collabsed' => TRUE,
  ];
  $form['head_footer_hiding_urls']['url'] = [
    '#type' => 'textarea',
    '#title' => t('Set url\'s'),
    '#description' => t('Set url\'s to hide header and footer,
 you can only write one url at a line.'),
    '#default_value' => (theme_get_setting('url') != NULL) ? theme_get_setting('url') : '',
  ];

  return $form;
}

How can I do that?

0

I found a solution, I wrote this in a preprocess function:

function THEME_preprocess_html(&$vars) {
  // Add body class if current url is configured to have header/footer hidden.
  $urls_to_test = array(
    $_GET['q'],
    drupal_get_path_alias($_GET['q']),
  );
  foreach (explode("\n", theme_get_setting('head_footer_hiding_urls')) as $url) {
    if (in_array($url, $urls_to_test)) {
      $vars['classes_array'][] = 'theme-hide-header-footer';
    }
  }
}
| improve this answer | |
  • And then you hide it via CSS? Wouldn't it be better to completely unset the region/block/or whatever this is via some more specific hook? – leymannx Jul 17 '19 at 20:42
  • 1
    Not in this case because I’m gonna do some stuff in JS, before hiding it, only if that body class if there, but otherwise it would definitely be a preferred solution. – user72672 Jul 17 '19 at 20:46
  • Like check if screen width is less than ... etc. – user72672 Jul 17 '19 at 20:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy