1

I am trying to write a custom module, which defines a field formatter plugin, but when I visit the Manage display page, I get this error:

Fatal error: Cannot declare class Drupal\my_formatter\Plugin\Field\FieldFormatter\MyFormatter, because the name is already in use in /var/www/sites/drupalroot/web/modules/custom/my_formatter/src/Plugin/Field/FieldFormatter/MyFormatter.php on line 0

I've compared my module to several other working field formatter plugins and can't seem to figure out what I'm missing.

My module directory is laid out like this:

web/modules/custom/my_formatter/
├── my_formatter.info.yml
├── my_formatter.module
├── src
│   └── Plugin
│       └── Field
│           └── FieldFormatter
│               └── MyFormatter.php
└── templates
    └── my-formatter.html.twig

My info file:

name: My Formatter
type: module
description: 'Do some fun stuff.'
package: Field types
core: 8.x
dependencies:
  - field
  - file

And my MyFormatter.php file:

<?php
/**
 * @file
 * Contains \Drupal\my_formatter\Plugin\Field\FieldFormatter\MyFormatter.
 */

namespace Drupal\my_formatter\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\FieldItemListInterface;
use Drupal\file\Plugin\Field\FieldFormatter\FileFormatterBase;

/**
 * Plugin implementation of the 'my' formatter.
 *
 * @FieldFormatter(
 *   id = "my",
 *   label = @Translation("My Formatter"),
 *   field_types = {
 *     "file"
 *   }
 * )
 */
class MyFormatter extends FileFormatterBase {

  /**
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode) {
    $elements = [];
    foreach ($items as $delta => $item) {
      $elements[$delta] = [
        '#theme' => 'my_formatter',
        '#my_var' => 'Hello',
      ];
    }
    return $elements;
  }

}

Any help would be greatly appreciated.

3
  • Usually I get that error if a class has a namespace that doesn't match the directory structure (i.e. I copy/paste a class and forget to update the namespace line), but what you have here looks correct to me.
    – sonfd
    Commented Sep 23, 2019 at 12:29
  • 1
    I've copied down your code and they work just fine for me. I'm able to go to the manage display page, choose the new formatter and save, no problem. You probably have some cached stuff or duplicated code somewhere? Error says it's already in use...
    – cchen
    Commented Sep 25, 2019 at 18:38
  • what is the exact name of your class MyFormatter if you copied the class from else where and let it with the same name it may cause this take a llok at this drupal.org/project/taxonomy_access_fix/issues/2943950 i know it's different from your case but the same issue, they just change the class name in the patch, try with change MyFormatter to MyFormatterTest and see if it works
    – berramou
    Commented Sep 26, 2019 at 11:00

2 Answers 2

3
+25

Here are some pointers that might help:

  • Run drush cr to clear cache and see if the error persists.
  • Enable error backtraces from the Config > Development > Logging & Errors page to see if you get more info in the error message about where the class is being redeclared.
  • Search your project for the name MyFormatter and see if you find anything besides the MyFormatter.php file you've mentioned.
  • File, directory and class names are case-sensitive – in what you've shared, they seem to be correctly named.
  • Make sure you're not manually using require or include anywhere to include this file.
    • Probably, not applicable in your case.
1
  • I think @jigarius comment regarding case sensitivity including the directory name is a good bet to look into. I received a similar error before using drush because I went to the directory /var/www/site/... instead of /var/www/Site/.... Of course this was in the command line and not the browser, so perhaps you need to look at the web server settings and make sure the document root has the right case?
    – Evil E
    Commented Oct 2, 2019 at 16:46
3

This error is often caused by a "stale" autoloader file. The first and most effective step would be to call

composer dump-autoload

from inside your drupal installation folder (presumably /var/www/sites/drupalroot/) in order to force an updating of the autoloader file in your vendor folder. Try clearing the composer caches as well in combination with this command:

composer clear-cache

Good luck!

P.S.: @see dump-autoload-dumpautoload for more information

3
  • Hmm. I wonder if composer's autoload deals with custom classes.
    – Jigarius
    Commented Oct 2, 2019 at 15:47
  • What is a custom class? Commented Oct 2, 2019 at 18:23
  • I mean the classes that are part of custom modules as opposed to classes which are managed by composer (e.g. the classes in the vendor directory).
    – Jigarius
    Commented Oct 3, 2019 at 1:09

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.