4

I am trying to port my Drupal 7 module, Views Hybrid, to Drupal 8. It provides a custom field formatter. In order to provide the field formatter, I am using the Doctrine annotation roughly following this tutorial provided by realize.be. However, I am getting PHP exceptions:

Uncaught PHP Exception Doctrine\Common\Annotations\AnnotationException: "[Syntax Error] Expected PlainValue, got '>' at position 168 in class Drupal\views_hybrid\Plugin\Field\FieldFormatter\HybridFormatter." at ~/core/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/AnnotationException.php line 41

If I remove the double quotes from the keys inside the settings array then I get a different error:

Uncaught PHP Exception Doctrine\Common\Annotations\AnnotationException: "[Semantical Error] Couldn't find constant items_count, class Drupal\views_hybrid\Plugin\Field\FieldFormatter\HybridFormatter." at ~/core/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/AnnotationException.php line 52

/**
 * Plugin implementation of the 'views_hybrid' formatter.
 *
 * @FieldFormatter(
 *   id = "views_hybrid",
 *   label = @Translation("Views hybrid"),
 *   field_types = {"entity_reference"},
 *   settings = {
 *      "items_count" => "5",
 *      "view" => "frontpage",
 *      "display" => "linked_title_only",
 *      "classes" => {},
 *      "view_mode" => "teaser",
 *      "arguments" => "",
 *   },
 * )
 */

I am not clear about the syntax that Doctrine expects for these nested arrays, or even on whether Drupal 8 expects the settings array to be included in the Doctrine annotations block when declaring a new field formatter, and my usually debugging techniques aren't very useful for debugging annotations since they're not exactly code. What is the expected syntax and content of a Doctrine annotation for defining a field formatter in Drupal 8?

1 Answer 1

3

I confused PHP array syntax for annotation syntax. Those => should be = and the trailing commas need to be removed from lines that are the last in their set. So it should be:

    /**
     * Plugin implementation of the 'views_hybrid' formatter.
     *
     * @FieldFormatter(
     *   id = "views_hybrid",
     *   label = @Translation("Views hybrid"),
     *   field_types = {"entity_reference"},
     *   settings = {
     *      "items_count" = "5",
     *      "view" = "frontpage",
     *      "display" = "linked_title_only",
     *      "classes" = {},
     *      "view_mode" = "teaser",
     *      "arguments" = ""
     *   }
     * )
     */
2
  • 2
    I reckon more than a few people will make this tiny mistake as D8 picks up speed, having a canonical question/answer about is is a good idea IMO. @betherwisser if you could maybe flesh the answer out a tiny bit with the 'why' (annotation syntax is a different beast to php array syntax, yada yada) I think it'd be even more helpful
    – Clive
    Commented Sep 2, 2014 at 23:55
  • I have edited to include your suggestion.
    – beth
    Commented Sep 3, 2014 at 15:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.