Собственные настройки темы в Друпал 7

31 января 2015 - 23:53

Для создания дополнительных настроек темы оформления сайта в Drupal 7 необходимо в папке с темой создать файл theme-settings.php и в нем с помощью хука hook_form_FORM_ID_alter() в форму настроек темы добавить свои поля. Следующий код добавляет в настройки темы ссылки на социальные сети:

<?php

/**
 * Implements hook_form_FORM_ID_alter().
 */

function THEME_form_system_theme_settings_alter(&$form, &$form_state) {

  $form['THEME'] = array(
    '#type' => 'fieldset',
    '#title' => t('THEME Theme Settings'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
  );
  $form['THEME']['socialicon'] = array(
    '#type' => 'fieldset',
    '#title' => t('Social Icons'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['THEME']['socialicon']['socialicon_display'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show Social Icon'),
    '#default_value' => theme_get_setting('socialicon_display', 'THEME'),
    '#description'   => t("Check this option to show Social Icons. Uncheck to hide."),
  );
  $form['THEME']['socialicon']['facebook_url'] = array(
    '#type' => 'textfield',
    '#title' => t('Facebook Profile URL'),
    '#default_value' => theme_get_setting('facebook_url', 'THEME'),
    '#description'   => t("Enter your Facebook Profile URL. Leave blank to hide."),
  );
  $form['THEME']['socialicon']['vk_url'] = array(
    '#type' => 'textfield',
    '#title' => t('Vkontakte Profile URL'),
    '#default_value' => theme_get_setting('vk_url', 'THEME'),
    '#description'   => t("Enter your Vkontakte Profile URL. Leave blank to hide."),
  );
  $form['THEME']['socialicon']['twitter_url'] = array(
    '#type' => 'textfield',
    '#title' => t('Twitter Profile URL'),
    '#default_value' => theme_get_setting('twitter_url', 'THEME'),
    '#description'   => t("Enter your Twitter Profile URL. Leave blank to hide."),
  );
}

Тогда вывести ссылки в нужном месте шаблона page.tpl.php (или любого другого шаблона темы) можно с помощью следующего кода:

  <?php if (theme_get_setting('socialicon_display', 'THEME')): ?>
      <?php
        $facebook_url = check_plain(theme_get_setting('facebook_url', 'THEME'));
        $vk_url = check_plain(theme_get_setting('vk_url', 'THEME'));
        $twitter_url = check_plain(theme_get_setting('twitter_url', 'THEME'));  
        $theme_path_social = base_path() . drupal_get_path('theme', 'THEME');
      ?>
        <ul class="social-icons">
          <?php if ($facebook_url): ?>
            <li>
              <a href="<?php print $facebook_url; ?>" target="_blank">
                <img src="<?php print $theme_path_social; ?>/images/facebook-icon.png">
              </a>
            </li>
          <?php endif; ?>
          <?php if ($vk_url): ?>
            <li>
              <a href="<?php print $vk_url; ?>" target="_blank">
                <img src="<?php print $theme_path_social; ?>/images/vk-icon.png">
              </a>
            </li>
          <?php endif; ?>
          <?php if ($twitter_url): ?>
            <li>
              <a href="<?php print $twitter_url; ?>" target="_blank">
                <img src="<?php print $theme_path_social; ?>/images/twitter-icon.png">
              </a>
            </li>
          <?php endif; ?>
        </ul>
    <?php endif; ?>