Problem/Motivation

Limited extending of list of variables. Use include files for that is not a solution.

Proposed resolution

Move variables definitions into GlobalVariable plugins.

API changes

New posibility define variables using Global Variable plugin.

Original report by voleger

// Social Sharing
  include 'includes/social-sharing.php';

This is not the Drupal way.
The easiest way to move functionality out of hook_preprocess is to use Services.

Comments

voleger created an issue. See original summary.

voleger’s picture

Status: Active » Needs review
StatusFileSize
new14.61 KB

So here the refactored module that uses services.

voleger’s picture

But still, this is not really good implementation of moving variables implementation out of the hook.
I have some work in progress that will be moving each variable in the separate plugin.
So the implementation of variable will looks like that:


namespace Drupal\mgv\Plugin\GlobalVariable;

use Drupal\mgv\Plugin\GlobalVariableInterface;

/**
 * Class CurrentPath.
 *
 * @package Drupal\mgv\Plugin\GlobalVariable
 *
 * @Mgv(
 *   name = "current_path",
 * );
 */
class CurrentPath implements GlobalVariableInterface {

  /**
   * {@inheritdoc}
   */
  public function getValue(array $deps = []) {
    // Print the current path. This could be useful if you want to do a redirect
    // after a form is submitted, e.g. ?destination={{ current_path }}.
    return \Drupal::service('path.current')->getPath();
  }

}
voleger’s picture

Issue summary: View changes
StatusFileSize
new23.8 KB
new26.79 KB

Here a completed patch that moves variables into separate plugins.
So developers can simply define a variable using GlobalVariable plugin in own module and variable will appear in global_variables list.

voleger’s picture

Title: Use services instead of included files. » Use plugins instead of included files.
markconroy’s picture

Hi volegar,

Thanks a lot for that. It's a huge improvement on what I had there previously. I'm going to make a new release of it now.

  • markconroy committed 19e87f7 on 8.x-1.x authored by voleger
    Issue #2885001 by voleger, markconroy: Use plugins instead of included...
markconroy’s picture

Status: Needs review » Fixed

Patch applied. Works lovely. Thanks.

markconroy’s picture

Status: Fixed » Closed (fixed)

Marking as closed, fixed.