PHP Attributes not shown for Plugin source code

Created on 18 November 2024, 5 months ago

This appears to happen for all plugins that have a PHP Attribute. Which is pretty much all plugins - only a few haven't been converted from Annotations to Attributes.

The source code of the plugins does not show the Attribute at all.

Example:

The Integer field formatter source code is displayed here:

https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Field%21P...

On api.drupal.org, the top of that class is currently shown like this (truncated for clarity):

<?php

namespace Drupal\Core\Field\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\Attribute\FieldFormatter;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Plugin implementation of the 'number_integer' formatter.
 *
 * The 'Default' formatter is different for integer fields on the one hand, and
 * for decimal and float fields on the other hand, in order to be able to use
 * different settings.
 */
class IntegerFormatter extends NumericFormatterBase {
    
    /**
     * {@inheritdoc}
     */
    public static function defaultSettings() {
        return [
            'thousand_separator' => '',
            'prefix_suffix' => TRUE,
        ] + parent::defaultSettings();
    }

What I expect to see is this (again truncated):

<?php

namespace Drupal\Core\Field\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\Attribute\FieldFormatter;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Plugin implementation of the 'number_integer' formatter.
 *
 * The 'Default' formatter is different for integer fields on the one hand, and
 * for decimal and float fields on the other hand, in order to be able to use
 * different settings.
 */
#[FieldFormatter(
  id: 'number_integer',
  label: new TranslatableMarkup('Default'),
  field_types: [
    'integer',
  ],
)]
class IntegerFormatter extends NumericFormatterBase {

  /**
   * {@inheritdoc}
   */
  public static function defaultSettings() {
    return [
      'thousand_separator' => '',
      'prefix_suffix' => TRUE,
    ] + parent::defaultSettings();
  }

The difference is that api.drupal.org doesn't display the PHP Attribute:

#[FieldFormatter(
  id: 'number_integer',
  label: new TranslatableMarkup('Default'),
  field_types: [
    'integer',
  ],
)]

Note that the api class documentation used to display a paragraph with the annotation, but now that we've switched to attributes that's missing.
See https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Field%21P...

In the D9 version you can see the annotation paragraph:

Plugin annotation

@FieldFormatter(
  id = "number_integer",
  label = @Translation("Default"),
  field_types = {
    "integer"
  }
)

But if you switch to D10 or D11 on that same page, the class now uses Attributes and this paragraph isn't shown any more.

πŸ› Bug report
Status

Active

Version

2.0

Component

Other

Created by

πŸ‡ΊπŸ‡ΈUnited States tr Cascadia

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @tr
  • πŸ‡ͺπŸ‡ΈSpain fjgarlin

    I'd call this a duplicate of the related issues, and these are probably the most important issues that we currently have with the transition to PHP attributes. I won't close it so that when the work is done we can check issue by issue that all possible cases are fixed.

  • πŸ‡ΊπŸ‡ΈUnited States tr Cascadia

    Yes I see now that this has been mentioned before. Sorry for the duplication.

    So there are two things I would like to make sure get addressed:

    • The old "Plugin annotation" documentation paragraph should be replaced by a "Plugin attribute" documentation, on the page where the plugin class is documented.
    • The source code pages and links should show the complete source code. I don't really understand the motivation for how it's currently done, with parsed-then-reconstructed source code being shown, because that leads to many problems like the missing Attributes and the changes to indentation, among others. This is a bigger problem than just the missing Attribute, because the current way is subject to break every time something new (like Attributes) is introduced into Drupal. IMO it would be better to display the raw source, or the raw source filtered to add hyperlinks, rather than a parsed-and-reconstructed source that will have problems every time the Parser doesn't do the right thing.
  • πŸ‡ͺπŸ‡ΈSpain fjgarlin

    That's the thing, we need to parse and reconstruct so all the links can be populated correctly, otherwise showing the code with no links would be somewhat useless. That parsing and reconstructing is currently the "heart" of the API project. I'm sure that there are better ways to do the things, but for now, that part of the module is unlikely to change. We "just" need to add support for new features.

Production build 0.71.5 2024