Commit c21fc824 authored by Thirsty Six's avatar Thirsty Six Committed by Benjamin Melançon
Browse files

Issue #3204260 by ThirstySix, mlncn: Make configurable, flexible & Drupal 9

parent 4fd0de51
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -2,4 +2,4 @@ name: Region In Content Templates
type: module
description: "Provides the ability to print regions (the building block of Drupal site building— where blocks are placed) within content (node) templates."
package: "Theme"
core: 8.x
core_version_requirement: ^8.8.0 || ^9.0
 No newline at end of file
+5 −0
Original line number Diff line number Diff line
regionincontent.admin:
  title: 'Region in Content settings'
  description: 'Configuration for Region in content.'
  parent: system.admin_config_ui
  route_name: regionincontent.admin_settings_form
 No newline at end of file
+8 −3
Original line number Diff line number Diff line
<?php

use \Drupal\block\Entity\Block;
use Drupal\block\Entity\Block;

/**
* Implements hook_preprocess_node() for node (content) templates.
@@ -9,11 +9,16 @@ function regionincontent_preprocess_node(&$variables) {
  // For performance we'll only add the desired region for set view modes.
  // This and the regions below could be made configurable through an admin UI.
  $allowed_view_modes = ['full'];

  $view_mode = $variables['view_mode'];

  if (in_array($view_mode, $allowed_view_modes)) {
    $allowed_regions = ['secondary_menu'];
    // Get the allowed regions from the settings
    $regionincontent_settings = \Drupal::config('regionincontent.settings');
    $regions = $regionincontent_settings->get('region', '');
    $allowed_regions_array = explode("\n", $regions);
    foreach ($allowed_regions_array as $key => $value) {
      $allowed_regions[] = trim($value);
    }
    regionincontent_add_regions_to_node($allowed_regions, $variables);
  }
}
+7 −0
Original line number Diff line number Diff line
regionincontent.admin_settings_form:
  path: '/admin/config/user-interface/regionincontent'
  defaults:
    _form: '\Drupal\regionincontent\Form\Regionincontent'
    _title: 'Region in Content Templates Settings'
  requirements:
    _permission: 'administer site configuration'
 No newline at end of file
+86 −0
Original line number Diff line number Diff line
<?php

/**
 * @file
 * Contains \Drupal\regionincontent\Form\SettingsForm.
 * Regionincontent settings form.
 */

namespace Drupal\regionincontent\Form;

use Drupal\Core\Form\ConfigFormBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\FormStateInterface;

/**
 * Defines a form that configure settings.
 */
class Regionincontent extends ConfigFormBase {

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('config.factory')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'regionincontent_admin_settings_form';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'regionincontent.settings',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL) {

    $form_state->disableCache();

    $region_config = $this->config('regionincontent.settings');

    $form['regionincontent'] = array(
      '#type'   => 'details',
      '#title'  => $this->t('Configuration'),
      '#open'   => TRUE,
      'region'  => array(
        '#type'          => 'textarea',
        '#title'         => $this->t('Region'),
        '#default_value' => $region_config->get('region') ? $region_config->get('region') : '',
        '#description'   => $this->t('Region ID as a separate line (example: primary_menu)'),
      ),
    );
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    /* ... */
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state->getValues();
    $this->config('regionincontent.settings')    
      ->set('region', $values['region'])
      ->save();
  }
}
 No newline at end of file