debugacademy / proj-me

GNU General Public License v2.0
0 stars 0 forks source link

Make the theme's header image configurable via a settings page #64

Open AliHassan7 opened 7 years ago

AliHassan7 commented 7 years ago
AliHassan7 commented 7 years ago

Replace your theme's proj_me_theme.theme code with the following code:

<?php
/**
 * @file
 * Bootstrap sub-theme.
 *
 * Place your custom PHP code in this file.
 */

function proj_me_theme_preprocess(&$variables) {
  $variables['year'] = date('Y');
  $variables['site_name'] = \Drupal::config('system.site')->get('name');
  $hero_image = theme_get_setting('proj_me_header');
  if (!empty($hero_image)) {
  $hero_file = \Drupal\file\Entity\File::load($hero_image[0]);
  $variables['hero_file_url'] = file_create_url($hero_file->getFileUri());
  }
}

function proj_me_theme_form_system_theme_settings_alter(&$form, \Drupal\Core\Form\FormStateInterface &$form_state) {
    $form['proj_me_header'] = array(
    '#type' => 'managed_file',
    '#default_value' => theme_get_setting('proj_me_header'),
    '#title' => t('Hero Image'),
    '#description' => t('Upload a file, allowed extensions: jpg, jpeg, png, gif'),
    '#upload_location' => 'public://'
  );
  return $form;
}
AliHassan7 commented 7 years ago

In your theme's page--front.html.twig file, replace the div that has an id of hero with the following code:

<div id="hero"{% if hero_file_url %} style="background-image: url('{{hero_file_url}}');"{% endif %}>
msheikh12 commented 7 years ago

completed https://github.com/debugacademy/proj-me/pull/70