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

jaypan’s picture

Your namespace should be Drupal\cerebro_item_link\Plugin\Field\FieldType

Contact me to contract me for D7 -> D10/11 migrations.

aaronmchale’s picture

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

namespace Drupal\cerebro_item_link\Plugin\Field\FieldWidget;

use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Plugin implementation for the Cerebro Item Link widget.
 *
 * @FieldWidget(
 *   id = "cerebro_item_link",
 *   module = "cerebro_item_link",
 *   label = @Translation("Item ID"),
 *   field_types = {
 *     "cerebro_item_link"
 *   }
 * )
 */
class CerebroItemLinkWidget extends WidgetBase implements WidgetInterface {

  /**
   * {@inheritdoc}
   */
  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
    // code here
  }

}

I've also tried setting the id to "cerebro_item_link_widget" but same problem, any help would be appreciated.

Thanks
Aaron

aaronmchale’s picture

Hi still looking for info/help with this, thanks

alen simonyan’s picture

Thank You Very Much, Namespace helps

b.ravanbakhsh’s picture

"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

plugin.manager.cerebro_item_link_widget:
  class: Drupal\cerebro_item_link_widget\Plugin\Field\FieldType\CerebroItemLinkField
  parent: default_plugin_manager
jaypan’s picture

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:

  1. The namespace is incorrect
  2. The class name is incorrect
  3. The filename is incorrect
  4. The file location does not match the namespace

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.

therobyouknow’s picture

  1. The namespace is incorrect
  2. The class name is incorrect
  3. The filename is incorrect
  4. The file location does not match the namespace

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.php

but in this file the classname is different to the filename:

class MyOtherFieldType extends FieldItemBase {

So in this case the filename, MyFieldType.php is not the same as the class name, MyOtherFieldType

So the correct way is: filename, MyFieldType.php and the class name is MyFieldType

(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.

jaypan’s picture

web/modules/custom/saf/src/Plugin/Field/FieldType/myFieldType.php

Note that class names should be capitalized. So myFieldType should be MyFieldType.

Contact me to contract me for D7 -> D10/11 migrations.

therobyouknow’s picture

Thank you @Jaypan - updated.