Resource plugin manager is creating the method

<?php
/**
   * Overrides Drupal\Component\Plugin\PluginManagerBase::getInstance().
   */
  public function getInstance(array $options){
    if (isset($options['id'])) {
      return $this->createInstance($options['id']);
    }
  }
?>

And

<?php
 $plugin = $this->restPluginManager->getInstance(['id' => $key]);
?>

is calling it.

However this can be simplified to

<?php
$plugin = $this->restPluginManager->createInstance($key);
?>

and then the getInstance override can be removed.

Patch incoming.

Comments

kylebrowning created an issue. See original summary.

kylebrowning’s picture

StatusFileSize
new1.46 KB
kylebrowning’s picture

Status: Needs review » Needs work

The last submitted patch, 2: rest-needlessmethod-2579235-1.patch, failed testing.

The last submitted patch, 2: rest-needlessmethod-2579235-1.patch, failed testing.

eclipsegc’s picture

wtf?

kylebrowning’s picture

I know right @EclipseGC

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 2: rest-needlessmethod-2579235-1.patch, failed testing.

willzyx’s picture

Status: Needs work » Needs review
StatusFileSize
new1.02 KB
new2.68 KB
kylebrowning’s picture

I didn't see those two places @willzyx, thanks.

kylebrowning’s picture

StatusFileSize
new2.75 KB

I guess lets keep the use of id consistent?

kylebrowning’s picture

StatusFileSize
new813 bytes

Interdiff wasn't added for some reason, sorry.

eclipsegc’s picture

Status: Needs review » Reviewed & tested by the community

This looks like a really big win, is ridiculously simple and also a better usage of Plugins in general.

Eclipse

alexpott’s picture

Version: 8.0.x-dev » 8.1.x-dev
Category: Bug report » Task
Status: Reviewed & tested by the community » Postponed
Issue tags: +D8 patch release target

This looks like a task tbh and not a bug. Also can't see why this is a big win - yes less code is nice but there is no bug in the current code it is just clunkier. Afaics this doesn't meet https://www.drupal.org/core/beta-changes. Postponing to a patch release

kylebrowning’s picture

Yeah i suppose it is no 8.0.x but 8.1.x which is fine.

Cleaner saner code IMO should always be used.

wim leers’s picture

eclipsegc’s picture

How is this a duplicate? That issue specifies that getInstance() fatals on most plugin managers... this plugin manager actually implements getInstance() needlessly. Two very different things, no?

Eclipse

chi’s picture

Version: 8.1.x-dev » 8.2.x-dev
Status: Closed (duplicate) » Needs review

I would agree. This is just a minor cleanup not related to #2625554: getInstance() fatals on almost all plugin managers.

kylebrowning’s picture

Status: Needs review » Reviewed & tested by the community

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 12: 2579235-12.patch, failed testing.

kylebrowning’s picture

Status: Needs work » Reviewed & tested by the community
StatusFileSize
new2.74 KB

Re-roll.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 22: 2579235-21.patch, failed testing.

The last submitted patch, 22: 2579235-21.patch, failed testing.

The last submitted patch, 22: 2579235-21.patch, failed testing.

willzyx’s picture

Status: Needs work » Needs review
StatusFileSize
new3.78 KB
new926 bytes
kylebrowning’s picture

Status: Needs review » Reviewed & tested by the community
alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/rest/src/Plugin/Type/ResourcePluginManager.php
@@ -33,13 +33,4 @@ public function __construct(\Traversable $namespaces, CacheBackendInterface $cac
-  public function getInstance(array $options){
-    if (isset($options['id'])) {
-      return $this->createInstance($options['id']);
-    }
-  }

Imo we should be marking this as deprecated for Drupal 9... since removing this now is unnecessary and will break code that currently works.

alexpott’s picture

All the other changes are fine but the change outlined in #28 is not patch or minor release safe.

willzyx’s picture

Status: Needs work » Reviewed & tested by the community
StatusFileSize
new3.77 KB
new645 bytes

Restored ::getInstance() method and marked it as deprecated. Back to RTBC

kylebrowning’s picture

Great call on #28.

This looks done.

catch’s picture

Status: Reviewed & tested by the community » Needs review
+++ b/core/modules/rest/src/Plugin/Type/ResourcePluginManager.php
@@ -36,6 +36,10 @@ public function __construct(\Traversable $namespaces, CacheBackendInterface $cac
   /**
    * {@inheritdoc}
+   *
+   * @deprecated in Drupal 8.2.0, will be removed before Drupal 9.0.0.
+   *   Use Drupal\rest\Plugin\Type\ResourcePluginManager::createInstance()
+   *   instead.
    */

I'm not sure what it means to deprecate an interface method. This is from MapperInterface which is implemented by DefaultPluginManager

If we remove the override in 9.x but don't do anything else, then it'll still have the method, just inherited from DefaultPluginManager - will that not work then?

willzyx’s picture

I'm not sure what it means to deprecate an interface method. This is from MapperInterface which is implemented by DefaultPluginManager

We simply want to encourage developers to use the proper method to create an instance of resource plugins instead of relying on the unnecessary implementation of ResourcePluginManager::::getInstance().

If we remove the override in 9.x but don't do anything else, then it'll still have the method, just inherited from DefaultPluginManager - will that not work then?

it wont work, exactly like other plugin managers that not implement ::getInstance() see #2625554: getInstance() fatals on almost all plugin managers (but as said in #18 by Eclipse Gc the two issues are not a duplicate)

wim leers’s picture

Status: Needs review » Reviewed & tested by the community
Related issues: +#1894130: Make \Drupal\Core\Plugin\Mapper\MapperInterface optional for plugin managers

IOW: ResourcePluginManager mistakenly abuses \Drupal\Component\Plugin\Mapper\MapperInterface::getInstance() for the purpose that \Drupal\Component\Plugin\Factory\FactoryInterface::createInstance() exists for.

Also see #1894130: Make \Drupal\Core\Plugin\Mapper\MapperInterface optional for plugin managers, which explains:

\Drupal\Core\Plugin\MapperInterface is a general-purpose interface to get plugin 'singletons' based on arbitrary 'options'. It is by no means a proper API, and it's only used by a handful of plugin managers.

Because the $options parameter is so generic, and different plugin managers behave differently based on the array's contents, the interface is useless by definition.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/rest/src/Plugin/Type/ResourcePluginManager.php
@@ -36,6 +36,10 @@ public function __construct(\Traversable $namespaces, CacheBackendInterface $cac
+   * @deprecated in Drupal 8.2.0, will be removed before Drupal 9.0.0.
+   *   Use Drupal\rest\Plugin\Type\ResourcePluginManager::createInstance()
+   *   instead.

I think this needs to be more nuanced. As @catch points out this method might still exist in Drupal 9. What we're really doing here is deprecating its usage for creating new instances.

kylebrowning’s picture

@alexpott, can you be more specific on what I should do here?

dawehner’s picture

What about simply using: * @deprecated in Drupal 8.2.0
+ * Use Drupal\rest\Plugin\Type\ResourcePluginManager::createInstance()
+ * instead.

kylebrowning’s picture

Status: Needs work » Needs review
StatusFileSize
new3.73 KB

Status: Needs review » Needs work

The last submitted patch, 38: resource_plugin_manager-2579235-38.patch, failed testing.

kylebrowning’s picture

StatusFileSize
new3.1 KB

Re-roll, the only changes are the suggestions in #37

kylebrowning’s picture

Status: Needs work » Needs review
kylebrowning’s picture

Status: Needs review » Needs work

Crap, forgot to save a file.

kylebrowning’s picture

Status: Needs work » Needs review
StatusFileSize
new3.74 KB
kylebrowning’s picture

Status: Needs review » Reviewed & tested by the community

Putting back to RTBC since it passed.

  • catch committed e039fa2 on 8.2.x
    Issue #2579235 by kylebrowning, willzyx: Resource plugin manager...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.2.x, thanks!

wim leers’s picture

YAY, finally closing this one :)

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.