Skip to content
Snippets Groups Projects
Commit a668b2e6 authored by Ahmad Alyasaki's avatar Ahmad Alyasaki
Browse files

Issue #3443529: Alter config setting to render the banner

parent f1e36680
No related branches found
No related tags found
No related merge requests found
.seeds-page-banner {
position: relative;
img {
width: 100%;
}
.page-header {
border: 0;
position: absolute;
top: 50%;
transform: translate(0,-50%);
margin: 0;
}
}
\ No newline at end of file
$seeds-page-primary: #c7c6c6;
$seeds-page-secondary: #f9f9f9;
$seeds: #4d4d4f;
$seeds-page-red: #FF0066;
$seeds-page-green: #38D42D;
$seeds-page-blue: #00AAFF;
$white: #FFF;
$gray: #DDD;
$black: #000;
@import "./theme_variables";
@import "./miscellaneous";
var gulp = require("gulp"),
sass = require("gulp-sass"),
autoprefixer = require("gulp-autoprefixer"),
livereload = require("gulp-livereload"),
sourcemaps = require("gulp-sourcemaps");
gulp.task("sass", function () {
gulp
.src(["./assets/scss/seeds-page.scss"])
.pipe(sourcemaps.init())
.pipe(sass().on("error", sass.logError))
.pipe(autoprefixer("last 2 version"))
.pipe(sourcemaps.write("./"))
.pipe(gulp.dest("./assets/css"));
});
gulp.task("build", function () {
gulp.start(["sass"]);
});
gulp.task("watch", ["sass"], function () {
livereload.listen();
gulp.watch("./assets/scss/**/*.scss", ["sass"]);
gulp.watch(["./assets/css/seeds-page.css"], function (files) {
livereload.changed(files);
});
});
seeds_page.config:
title: "Seeds Page"
description: "Manage seeds page configuration."
route_name: seeds_page.seeds_page_form
parent: system.admin_config_content
......@@ -20,3 +20,76 @@ function seeds_page_help($route_name) {
return '';
}
}
/**
* Implements hook_theme().
*/
function seeds_page_theme($existing, $type, $theme, $path) {
$templates['seeds_page_field_paragraph__tabs'] = [
'base hook' => 'field',
];
$templates['seeds_banner'] = [
'render element' => 'content',
'variables' => [
'image' => NULL,
'title' => NULL,
'show_title' => NULL,
],
];
return $templates;
}
/**
* Implements hook_preprocess_HOOK().
*/
function seeds_page_preprocess_page(&$variables) {
// Do not do anything in an admin page.
if (\Drupal::service('router.admin_context')->isAdminRoute()) {
return;
}
/** @var \Drupal\seeds_page\SeedsPageManager $seeds_page_manager */
$seeds_page_manager = \Drupal::service('seeds_page.manager');
$route_match = \Drupal::routeMatch();
$config = \Drupal::config('seeds_page.config');
$entity = $seeds_page_manager->getEntityFromCurrentPage();
$always_render_banner = $config->get('always_render_banner');
// Get the field to render.
$field = $config->get('field') ?: 'field_image';
// Set the media id.
if (isset($entity->{$field}->entity)) {
$media_id = $entity->{$field}->entity->id();
}
else {
// Get media id from config.
$media_id = $config->get('media_id');
}
if (($entity || $always_render_banner) && $media_id) {
// Load media from media id.
$media = \Drupal::entityTypeManager()->getStorage('media')->load($media_id);
// Render media using banner view mode.
$image_builder = \Drupal::entityTypeManager()->getViewBuilder('media')->view($media, 'seeds_banner');
// Get the page title.
$request = \Drupal::request();
$page_title = \Drupal::service('title_resolver')->getTitle($request, $route_match->getRouteObject());
// Set it in the variables.
$variables['seeds_banner'] = [
'image' => $image_builder,
'page_title' => $page_title,
'show_title' => $config->get('show_title'),
];
// Render the image if it is allowed.
$render_the_banner = $config->get('render_banner');
if ($render_the_banner) {
$banner = [
'#theme' => 'seeds_banner',
'#image' => $image_builder,
'#title' => $page_title,
'#show_title' => $config->get('show_title'),
];
$content = $variables["page"]["content"];
$variables["page"]["content"] = ['seeds-banner' => $banner] + $content;
}
}
}
seeds_page.seeds_page_form:
path: "/admin/config/content/seeds_page"
defaults:
_form: '\Drupal\seeds_page\Form\SeedsPageForm'
_title: "Seeds Page"
requirements:
_permission: "administer seeds page"
......@@ -92,6 +92,23 @@ class SeedsPageForm extends ConfigFormBase {
$form['media_wrapper'] = [
'#type' => 'container',
];
$form['media_wrapper']['media'] = [
'#type' => 'entity_autocomplete',
'#target_type' => 'media',
'#title' => $this->t('Default Media'),
'#description' => $this->t('Select a media image to provide as a default banner.'),
'#default_value' => $media,
'#selection_settings' => [
'target_bundles' => ['image'],
],
'#weight' => '0',
];
$form['media_wrapper']['html'] = [
'#markup' => 'Use the following variables in preprocess page:
<ul><li>seeds_banner.image</li><li>seeds_banner.page_title</li></ul>
',
];
$form['field'] = [
'#type' => 'select',
......@@ -100,6 +117,23 @@ class SeedsPageForm extends ConfigFormBase {
'#default_value' => $config->get('field'),
];
$form['always_render_banner'] = [
'#type' => 'checkbox',
'#title' => $this->t('Always render the banner'),
'#default_value' => $config->get('always_render_banner'),
];
$form['entity_types_wrapper'] = [
'#type' => 'fieldset',
'#title' => $this->t('Bundles'),
'#tree' => TRUE,
'#states' => [
'visible' => [
'input[name="always_render_banner"' => ['checked' => FALSE],
],
],
];
$entity_types = $this->loadAllEntityTypes();
foreach ($entity_types as $entity_type_id => $entity_type) {
if ($this->seedsPageManager->isAcceptedEntity($entity_type_id)) {
......@@ -113,6 +147,20 @@ class SeedsPageForm extends ConfigFormBase {
}
}
$form['render_banner'] = [
'#type' => 'checkbox',
'#title' => $this->t('Render The Banner'),
'#description' => $this->t('If you don\'t want to use twig, use this option to let the module render the banner.'),
'#default_value' => $config->get('render_banner'),
];
$form['show_title'] = [
'#type' => 'checkbox',
'#title' => $this->t('Show Title'),
'#description' => $this->t('If you don\'t want to show title, uncheck this option.'),
'#default_value' => $config->get('show_title'),
];
$form['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Submit'),
......@@ -178,8 +226,12 @@ class SeedsPageForm extends ConfigFormBase {
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this->configFactory->getEditable('seeds_page.config');
$config->set('media_id', $form_state->getValue('media'));
$config->set('render_banner', $form_state->getValue('render_banner'));
$config->set('show_title', $form_state->getValue('show_title'));
$config->set('entity_types', $form_state->getValue('entity_types_wrapper'));
$config->set('field', $form_state->getValue('field'));
$config->set('always_render_banner', $form_state->getValue('always_render_banner'));
$config->save();
parent::submitForm($form, $form_state);
......
......@@ -6,6 +6,7 @@ use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Image\ImageFactory;
use Drupal\Core\Render\Renderer;
use Drupal\Core\Routing\CurrentRouteMatch;
use Drupal\Core\Entity\FieldableEntityInterface;
/**
*
......@@ -78,4 +79,37 @@ class SeedsPageManager {
return in_array($entity_type_id, self::ACCEPTED_ENTITY_TYPES, TRUE);
}
/**
* Returns the node or term from current page.
*
* @return \Drupal\Core\Entity\EntityInterface
*/
public function getEntityFromCurrentPage() {
$always_render_banner = $this->configFactory->get('seeds_page.config')->get('always_render_banner');
if (!$always_render_banner) {
$route_name = $this->routeMatch->getRouteName();
$matches = [];
$entity_landing = preg_match('/entity\.([\w_]+)\.canonical/', $route_name, $matches);
$current_entity_type = $matches[1] ?? NULL;
/** @var \Drupal\Core\Entity\EntityInterface $entity */
$entity = $entity_landing ? $this->routeMatch->getParameter($current_entity_type) : NULL;
if ($entity_landing && $entity && $this->isAcceptedEntity($current_entity_type)) {
$entity_types = $this->configFactory->get('seeds_page.config')->get('entity_types');
$allowed_bundle = $entity_types[$current_entity_type][$entity->bundle()] ?? NULL;
if ($allowed_bundle) {
return $entity;
}
}
}
else {
$route_params = $this->routeMatch->getParameters();
foreach ($route_params as $param) {
if ($param instanceof FieldableEntityInterface) {
return $param;
}
}
}
return NULL;
}
}
<div class="seeds-page-banner">
<div class="seeds-banner">{{image}}</div>
{% if show_title %}
<div class="container">
<h1 class="page-header">{{title}}</h1>
</div>
{% endif %}
</div>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment