I've made a custom theme for Drupal 8. Now I want to provide a banner image for the front page. I wouldn't like to create a custom content type Banner
, then create a Banner-node and promote it to the front page, because the promoted node-teasers are arranged inside a grid-view in my theme (together with other node-teasers of other content types, such as Article
) and when I do it I do not get what I expect. So it is totally not what I would like to do.
So first I decided to provide a custom banner_image
field inside my_custom_theme.theme
file (by implementing the appropriate hooks):
function my_custom_theme_preprocess_page(&$variables)
{
$variables['banner_image'] = theme_get_setting(‘banner_image’);
}
function my_custom_theme_form_system_theme_settings_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state)
{
$form['my_custom_theme_settings']['website'] = array(
'#type' => 'details',
'#title' => t('Website Settings'),
);
$form['my_custom_theme_settings']['website']['banner_image'] = array(
'#type' => 'managed_file',
'#upload_location' => 'public://media/branding/',
'#upload_validators' => array(
'file_validate_extensions' => array('gif png jpg jpeg'),
),
'#default_value' => theme_get_setting('banner_image'),
);
}
And I've got following result (Admin Panel):
It means: uploading a banner image works. However, I think I have done everything to get the image on my front page, without any success (the banner image doesn't show up).
Therefore I overrode the page--front.html.twig
file:
<...>
{{ banner_image }}
<...>
No image, no Url, nothing (The image has been correctly uploaded to the appropriate location, the theme has been uninstalled, the cache has been cleared, and the theme has been reinstalled again).
What am I doing wrong? If it's impossible to do it that way, is there a way just to copy the same functionality of logo_image
and use it for my banner_image
?
1) By defining the banner_image
settings in the install\my_theme_settings.yml
file:
features:
logo: true
banner: true
2) By initializing a checked checkbox, such as for logo_image
:
3) By showing the upload-fields, such as for logo_image
, when the checkbox is unchecked: