Custom Context Plugins
Context plugins for ConfigPages build as all other plugins in Drupal 8.
So, first of all, you need to create a PHP Class file in proper place, it should be your_module/src/Plugin/ConfigPagesContext/
Let's create a simple Context example based on Domain module.
So we will create file my_module/src/Plugin/ConfigPagesContext/Domain.php
Then we need to set a namespace for our Context:
namespace Drupal\my_module\Plugin\ConfigPagesContext;
Next will be a Plugin definition which is made via annotations:
/**
* Provides a Domain config pages context.
*
* @ConfigPagesContext(
* id = "domain",
* label = @Translation("Domain"),
* )
*/
class Domain extends ConfigPagesContextBase{
So, now you can clear cache and on the ConfigPage edit page, you will see your new context has appeared.
Let's make it work.
There are 3 simple methods which you can override to make your plugin work.
First one is
/**
* Return the label of the context.
*
* @return string
*/
public function getLabel() {
This method is used to get human readable name of current context value, in our case we want to show domain name.
In this tutorial, I'll show ugly but fast method without DI ( but please use create method if you need to get some service injected)
/**
* Return the label of the context.
*
* @return string
*/
public function getLabel() {
$loader = \Drupal::service('domain.negotiator');
$current_domain = $loader->getActiveDomain();
return $current_domain->label();
}
After that, you can go to your ConfigPage edit page and you'll see something like this:
So, now we can see which context is used for our ConfigPage, let's move to context value. Context value is used to define which ConfigPage entity should be loaded for you based on some environment parameters.
/**
* Return the value of the context.
*
* @return mixed
*/
public function getValue() {
$loader = \Drupal::service('domain.negotiator');
$current_domain = $loader->getActiveDomain();
return $current_domain->id();
}
Last but not least: Context links.
The list of links which can be available on ConfigPage edit page (this will make your life easier and will allow you to switch between available contexts very fast). Please note that it's not necessary to implement this method in your plugin.
/**
* Return array of available links to switch on given context.
*
* @return array
*/
public function getLinks() {
$links = [];
$value = $this->getValue();
\Drupal::service('entity_type.manager')->getStorage('domain')->resetCache();
$domains = \Drupal::service('entity_type.manager')->getStorage('domain')->loadMultiple();
// Sorting domains.
$query = $domains->getQuery();
$query->sort('weight', 'ASC');
$result = $query->execute();
$domains = $domains->loadMultiple($result);
foreach ($domains as $domain) {
$links[] = [
'title' => $domain->label(),
'href' => Url::fromUri($domain->getUrl()),
'selected' => ($value == $domain->id()) ? TRUE : FALSE,
'value' => $domain->id(),
];
}
return $links;
}
Here we have standard 'title' and 'href' for URL, 'value' is used to organize items in BO (this is not visible to users but should be unique). 'selected' is a marker to make link bold, means it's your current context.
After all of this, you should see next options in BO.
The full code example can be found here: https://github.com/shumer/config_page_domain_context/blob/master/src/Plugin/ConfigPagesContext/Domain.php
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion