Hello
I've been following the Drupal Docs for creating a custom Field Type, but I've been stuck with this PHP Error for some time and haven't been able to find anything online that will help me overcome it.
PHP message: PHP Fatal error: Cannot declare class Drupal\CerebroItemLink\Plugin\Field\FieldType\CerebroItemLinkField, because the name is already in use in /var/www/drupal8/modules/cerebro_item_link/src/Plugin/Field/FieldType/CerebroItemLinkField.php on line 0
Structure of the file mentioned above, with standard override functions excluded as they were basically copied from the documentation.
namespace Drupal\CerebroItemLink\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\Form\FormStateInterface;
/**
* Provides a field type of CerebroItemLink.
*
* @FieldType(
* id = "cerebro_item_link",
* module = "cerebro_item_link",
* label = @Translation("Cerebro Item Link"),
* default_formatter = "cerebro_item_link_formatter",
* default_widget = "cerebro_item_link_widget",
* )
*/
class CerebroItemLinkField extends FieldItemBase {
}
I've read through the PHP Docs on namespaces and I can't see what I'm doing that is causing this issue, especially since the error is thrown at line 0, which in itself seems strange.
Any help with this would be appreciated,
Thanks
Aaron
Comments
The second element of your
Your namespace should be
Drupal\cerebro_item_link\Plugin\Field\FieldTypeContact me to contract me for D7 -> D10/11 migrations.
Hi jaypan, thanks for the
Hi jaypan, thanks for the reply, that seems to have addressed the error but I'm a little unsure as to why that would fix this specific error, since the error seems to be generated by PHP itself and I can't find any PHP documentation around that error which would suggest it must comply with any particular naming scheme. Of course my assumption is Drupal is doing something that is resulting in this error to be thrown when the namespace doesn't match what is expected but it seems a little strange. Are you or anyone else able to provide further clarification?
In addition I'm now getting a message when I try to create a new field of this type "There was a problem creating field Cerebro Item ID: The "cerebro_item_link_widget" plugin does not exist." But from what I can tell I've setup the plugin correctly based on the documentation, here is a copy of the file
I've also tried setting the id to "cerebro_item_link_widget" but same problem, any help would be appreciated.
Thanks
Aaron
Hi still looking for info
Hi still looking for info/help with this, thanks
Did you get to the bottom of this?
I've a similar issue, ref: https://www.drupal.org/forum/support/post-installation/2018-01-04/php-er...
Thank You Very Much,
Thank You Very Much, Namespace helps
Drupal\cerebro_item_link_widget\Plugin\Field\FieldType\CerebroIt
"cerebro_item_link_widget" does not exist means you need to declare it in your module services in MODULE_NAME.services.yml,
cerebroItem_link.services.yml
here is a sample code
I'll add a generic comment to
I'll add a generic comment to this thread - if you are getting this error, as far a I can tell it always comes down to one of the following:
If you are facing this error, double check each of those to make sure they are correct.
Contact me to contract me for D7 -> D10/11 migrations.
The namespace is incorrect
Thank you very much @Jaypan - this helped me (see below).
I'll add one more:
5. The filename does not match the classname (or vice versa), they just have to be the same
e.g.
filepath and name is
web/modules/custom/saf/src/Plugin/Field/FieldType/MyFieldType.phpbut in this file the classname is different to the filename:
class MyOtherFieldType extends FieldItemBase {So in this case the filename,
MyFieldType.phpis not the same as the class name,MyOtherFieldTypeSo the correct way is: filename,
MyFieldType.phpand the class name isMyFieldType(of course, use your own filename and classname. The key thing is that the filename (without the .php extension on the end) has to match the classname (or vice-versa).)
This issue can arise for example with a copy paste whereby not all the names after the copy was made are set correctly in the code and filename.
This solved my issue and @Jaypan's very useful comment with 4 points triggered me to check that. But just in case other folks didn't make that connection.
web/modules/custom/saf/src
web/modules/custom/saf/src/Plugin/Field/FieldType/myFieldType.phpNote that class names should be capitalized. So myFieldType should be MyFieldType.
Contact me to contract me for D7 -> D10/11 migrations.
Thank you @Jaypan - updated.
Thank you @Jaypan - updated.