I'm working on Drupal 8 and I create a custom field type so

When I tried to add a field to a content type Drupal give me this error

Fatal error: Cannot redeclare class Drupal\any_to_show\Plugin\Field\FieldType\AnyToShowItem in /var/www/public/sites/all/modules/something/src/Plugin/Field/FieldType/AnyToShowItem.php on line 105

I'm confused because it suppose that the file is the definition and it says that I'm redeclaring the class, I search whole site but nothing.

I was grateful if somebody can help me. thanks in advance

  • Did you generate this code with Drupal Console or did you write it from scratch? – Kevin Mar 8 '16 at 14:22
  • I do it from scratch using as reference the plugin telephone to make it right – Emiliano Mar 8 '16 at 14:24
  • Just a question, because I got similar error(s) before after generating code. Can you post your class so we can see the code? – Kevin Mar 8 '16 at 14:41

This happened to me, and the solution was making sure that the file name matches the class name defined within, so in your case, the class name MUST be AnyToShowItem.

This is because the PSR-4 auto-loader would be trying to load .../sites/all/modules/something/src/Plugin/Field/FieldType/AnyToShowItem.php if the class AnyToShowItem doesn't exist. But if the class defined within doesn't match the filename, then AnyToShowItem will still be undefined but the class that IS contained in the file will be. So the next time the AnyToShowItem class is attempted to be constructed, the file is loaded again in a futile attempt to define the class, but the class within will already have been defined, and thus the redefinition error will be thrown

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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