If a FieldEnhancer plugin does not provide any settings, when the enhancer is used on an entity's JSONAPI output the following error is thrown:

TypeError: Argument 2 passed to Drupal\Component\Plugin\PluginManagerBase::createInstance() must be of the type array, null given, called in modules/contrib/jsonapi_extras/src/Normalizer/FieldItemNormalizer.php on line 87 in Drupal\Component\Plugin\PluginManagerBase->createInstance() (line 71 of core/lib/Drupal/Component/Plugin/PluginManagerBase.php).
CommentFileSizeAuthor
#6 jsonapi_extras-n2878938-6.patch901 bytesdamienmckenna

Comments

DamienMcKenna created an issue. See original summary.

damienmckenna’s picture

Status: Active » Closed (works as designed)

Never mind, I hadn't implemented all of the methods.

damienmckenna’s picture

Status: Closed (works as designed) » Active

Yes, this is a bug after all :p

damienmckenna’s picture

Here's a FieldEnhancer that demonstrates the bug.


namespace Drupal\mymodule\Plugin\jsonapi\FieldEnhancer;

use Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerBase;
use Symfony\Component\Serializer\Encoder\JsonDecode;
use Symfony\Component\Serializer\Encoder\JsonEncoder;


/**
 * Output a field as a JSON object.
 *
 * @ResourceFieldEnhancer(
 *   id = "json_snippet",
 *   label = @Translation("JSON snippet"),
 *   description = @Translation("Output a field as a JSON object.")
 * )
 */
class JsonSnippet extends ResourceFieldEnhancerBase {

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    // The plugin system requires an empty array rather than NULL.
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function postProcess($value) {
    // This needs to be nested arrays rather than StdClass objects.
    $decoder = new JsonDecode(TRUE);
    return $decoder->decode($value, JsonEncoder::FORMAT, []);
  }

  /**
   * {@inheritdoc}
   */
  public function prepareForInput($value) {
    return $value;
  }

  /**
   * {@inheritdoc}
   */
  public function getJsonSchema() {
    // @todo Not sure what this is supposed to be.
    return [
      'type' => 'json',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getSettingsForm(array $resource_field_info) {
    return [];
  }

}

As you can see the FieldEnhancer has all of the required methods, but when you try to output something using it the error is thrown.

damienmckenna’s picture

This is what the config looks like on a field which uses the FieldEnhancer:

  field_myfield:
    fieldName: field_myfield
    publicName: myfield
    enhancer:
      id: json_snippet
    disabled: false
damienmckenna’s picture

Status: Active » Needs review
StatusFileSize
new901 bytes

This should fix the problem.

What was happening was that it wasn't checking what the $settings were before passing them into createInstance(), which is where it needed an array. This starts with an array and only uses the passed in settings if they're an array.

  • e0ipso committed 416c62f on 8.x-1.x authored by DamienMcKenna
    fix(Config): Allow FieldEnhancer plugin without settings (#2878938 by...
e0ipso’s picture

Status: Needs review » Fixed

Many thanks @DamienMcKenna!

Status: Fixed » Closed (fixed)

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