0

I have a custom plugin. As a new user, when I try to load existing plugin definitions, it returns empty.

The annotation definition:

<?php

namespace Drupal\cp_settings\Annotation;

use Drupal\Component\Annotation\Plugin;

/**
 * CP settings annotion.
 *
 * @Annotation
 */
class CpSetting extends Plugin {

}

Plugin manager:

<?php

namespace Drupal\cp_settings\Plugin;

/**
 * Class CpSettingsManager.
 */
class CpSettingsManager extends DefaultPluginManager implements CpSettingsManagerInterface {

  /**
   * {@inheritdoc}
   */
  public function __construct(\Traversable $namespaces, CacheBackendInterface $cacheBackend, ModuleHandlerInterface $module_handler) {
    parent::__construct(
      'Plugin/CpSetting',
      $namespaces,
      $module_handler,
      'Drupal\cp_settings\CpSettingInterface',
      'Drupal\cp_settings\Annotation\CpSetting'
    );

    $this->alterInfo('cp_settings');
    $this->setCacheBackend($cacheBackend, 'cp_settings_plugins');
  }

  public function getPluginsForGroup(string $group) {
    $defs = $this->getDefinitions();
    $defs = array_filter($defs, function ($a) use ($group) {
      return ($a['group']['id'] == $group);
    });

    $plugins = array_map(function ($a) {
      return $this->createInstance($a['id']);
    }, $defs);

    return $plugins;
  }

}

Example plugin implementation:

<?php

namespace Drupal\custom_module\Plugin\CpSetting;

/**
 * Plugin.
 *
 * @CpSetting(
 *   id = "custom_module_setting",
 *   title = @Translation("Custom module"),
 *   group = {
 *    "id" = "seo",
 *    "title" = @Translation("SEO"),
 *    "parent" = "cp.settings"
 *   }
 * )
 */
class CustomModuleSetting extends PluginBase implements CpSettingInterface {

  public function getForm(array &$form, ConfigFactoryInterface $configFactory) {
    // build form array.
  }

  public function access(AccountInterface $account): AccessResultInterface {
    // perform access check.
  }

}

The plugin is used like this:

<?php

namespace Drupal\cp_settings\Form;


/**
 * Class CpSettingsForm.
 */
class CpSettingsForm extends ConfigFormBase {

  protected function getPlugins() {
    $group = $this->getRequest()->get('setting_group');

    if (empty($this->plugins) && $group) {
      $this->plugins = $this->pluginManager->getPluginsForGroup($group);
    }

    return $this->plugins;
  }

  public function access(AccountInterface $account) {
    $access = AccessResult::neutral();
    foreach ($this->getPlugins() as $group => $cp) {
      $access = $access->orIf($cp->access($account));
    }
    return $access;
  }

  public function buildForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildForm($form, $form_state);

    $plugins = $this->getPlugins();
    foreach ($plugins as $p) {
      $p->getForm($form, $this->configFactory);
    }

    return $form;
  }

}

The problem is, for a new user $this->getPlugins() is empty, i.e. unable to load the CustomModuleSetting plugin implementation, and due to which it is unable to call \Drupal\custom_module\Plugin\CpSetting\CustomModuleSetting::access.


I found that in a new site, the cache_discovery table has an entry for cp_settings_plugins - but it is empty. But it is initiated with definitions, once I rebuild the cache.


I have referred the official docs, and everything looks fine. I am not sure what is causing the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.