1

I am maintaining a Drupal module that was developed by a past colleague. I am attempting to add my own custom field via the Drupal API. I am taking the object oriented approach extending FieldItemBase, WidgetBase, and FormatterBase (opposed to creating fields through the config .yml files).

I hit a few snags once I had all my code written up, and tried to add my field to an existing content type (basic page). What happened was once I had added my custom field via structure -> content types -> manage fields -> add field, I tried to add a basic page and put some data in the field.

I looked in the /admin/reports/dblog page and saw:

Drupal\Component\Plugin\Exception\PluginNotFoundException: The "int" plugin does not exist. in Drupal\Core\Plugin\DefaultPluginManager->doGetDefinition() (line 52 of /home/avery_ae/drupal-plugin.appreciationengine.com/core/lib/Drupal/Component/Plugin/Discovery/DiscoveryTrait.php).

Was thrown.

an error that seemed to result from an invalid type that I passed through in the FieldItemInterface::propertyDefinitions function. I changed my code from:

$properties['ae_segmentid'] = DataDefinition::create('int')->setLabel(t('SegmentID'))->setRequired(FALSE);

to

$properties['ae_segmentid'] = DataDefinition::create('integer')->setLabel(t('SegmentID'))->setRequired(FALSE);

(int to integer)

And that resolved that issue, so I figured I would clear the cache and remove that field through the UI. However when I attempted to remove it I was taken to a blank page with a lone message reading:

The website encountered an unexpected error. Please try again later.

So back to the logs, and saw this warning message (really long):

Warning: Illegal string offset 'ae_segmentid' in Drupal\Component\Utility\NestedArray::setValue() (line 155 of /home/avery_ae/drupal-plugin.appreciationengine.com/core/lib/Drupal/Component/Utility/NestedArray.php) #0 /home/avery_ae/drupal-plugin.appreciationengine.com/core/includes/bootstrap.inc(566): _drupal_error_handler_real(2, 'Illegal string ...', '/home/avery_ae/...', 155, Array) #1 /home/avery_ae/drupal-plugin.appreciationengine.com/core/lib/Drupal/Component/Utility/NestedArray.php(155): _drupal_error_handler(2, 'Illegal string ...', '/home/avery_ae/...', 155, Array) #2 /home/avery_ae/drupal-plugin.appreciationengine.com/core/lib/Drupal/Core/Form/FormBuilder.php(1241): Drupal\Component\Utility\NestedArray::setValue(Array, Array, NULL) #3 /home/avery_ae/drupal-plugin.appreciationengine.com/core/lib/Drupal/Core/Form/FormBuilder.php(986): Drupal\Core\Form\FormBuilder->handleInputElement('node_page_form', Array, Object(Drupal\Core\Form\FormState)) #4 /home/avery_ae/drupal-plugin.appreciationengine.com/core/lib/Drupal/Core/Form/FormBuilder.php(1056): Drupal\Core\Form\FormBuilder->doBuildForm('node_page_form', Array, Object(Drupal\Core\Form\FormState)) #5 /home/avery_ae/drupal-plugin.appreciationengine.com/core/lib/Drupal/Core/Form/FormBuilder.php(1056): Drupal\Core\Form\FormBuilder->doBuildForm('node_page_form', Array, Object(Drupal\Core\Form\FormState)) #6 /home/avery_ae/drupal-plugin.appreciationengine.com/core/lib/Drupal/Core/Form/FormBuilder.php(1056): Drupal\Core\Form\FormBuilder->doBuildForm('node_page_form', Array, Object(Drupal\Core\Form\FormState)) #7 /home/avery_ae/drupal-plugin.appreciationengine.com/core/lib/Drupal/Core/Form/FormBuilder.php(1056): Drupal\Core\Form\FormBuilder->doBuildForm('node_page_form', Array, Object(Drupal\Core\Form\FormState)) #8 /home/avery_ae/drupal-plugin.appreciationengine.com/core/lib/Drupal/Core/Form/FormBuilder.php(557): Drupal\Core\Form\FormBuilder->doBuildForm('node_page_form', Array, Object(Drupal\Core\Form\FormState)) #9 /home/avery_ae/drupal-plugin.appreciationengine.com/core/lib/Drupal/Core/Form/FormBuilder.php(314): Drupal\Core\Form\FormBuilder->processForm('node_page_form', Array, Object(Drupal\Core\Form\FormState)) #10 /home/avery_ae/drupal-plugin.appreciationengine.com/core/lib/Drupal/Core/Entity/EntityFormBuilder.php(48): Drupal\Core\Form\FormBuilder->buildForm('node_page_form', Object(Drupal\Core\Form\FormState)) #11 /home/avery_ae/drupal-plugin.appreciationengine.com/core/modules/node/src/Controller/NodeController.php(113): Drupal\Core\Entity\EntityFormBuilder->getForm(Object(Drupal\node\Entity\Node)) #12 [internal function]: Drupal\node\Controller\NodeController->add(Object(Drupal\node\Entity\NodeType)) #13 /home/avery_ae/drupal-plugin.appreciationengine.com/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array(Array, Array) #14 /home/avery_ae/drupal-plugin.appreciationengine.com/core/lib/Drupal/Core/Render/Renderer.php(582): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() #15 /home/avery_ae/drupal-plugin.appreciationengine.com/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(124): Drupal\Core\Render\Renderer->executeInRenderContext(Object(Drupal\Core\Render\RenderContext), Object(Closure)) #16 /home/avery_ae/drupal-plugin.appreciationengine.com/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) #17 [internal function]: Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() #18 /home/avery_ae/drupal-plugin.appreciationengine.com/vendor/symfony/http-kernel/HttpKernel.php(153): call_user_func_array(Object(Closure), Array) #19 /home/avery_ae/drupal-plugin.appreciationengine.com/vendor/symfony/http-kernel/HttpKernel.php(68): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1) #20 /home/avery_ae/drupal-plugin.appreciationengine.com/core/lib/Drupal/Core/StackMiddleware/Session.php(57): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #21 /home/avery_ae/drupal-plugin.appreciationengine.com/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(47): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #22 /home/avery_ae/drupal-plugin.appreciationengine.com/core/modules/page_cache/src/StackMiddleware/PageCache.php(99): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #23 /home/avery_ae/drupal-plugin.appreciationengine.com/core/modules/page_cache/src/StackMiddleware/PageCache.php(78): Drupal\page_cache\StackMiddleware\PageCache->pass(Object(Symfony\Component\HttpFoundation\Request), 1, true) #24 /home/avery_ae/drupal-plugin.appreciationengine.com/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(47): Drupal\page_cache\StackMiddleware\PageCache->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #25 /home/avery_ae/drupal-plugin.appreciationengine.com/modules/devel/webprofiler/src/StackMiddleware/WebprofilerMiddleware.php(38): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #26 /home/avery_ae/drupal-plugin.appreciationengine.com/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(50): Drupal\webprofiler\StackMiddleware\WebprofilerMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #27 /home/avery_ae/drupal-plugin.appreciationengine.com/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #28 /home/avery_ae/drupal-plugin.appreciationengine.com/core/lib/Drupal/Core/DrupalKernel.php(657): Stack\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #29 /home/avery_ae/drupal-plugin.appreciationengine.com/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request)) #30 {main}.

So I thought there may have been a mismatch in the naming conventions I used in the code. So I tried the best that I could to switch field, name, offset, and ID to ae_segmentid.

Well that seemed to cause a disaster because the next error messaged sounds to me like I orphaned some database records, and there is a serious disconnection between what is on the admin panel, the backend and the DB. This was the message:

Drupal\Core\Database\DatabaseExceptionWrapper: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'field_segmentid_test_ae_segmentid' in 'where clause': SELECT 1 AS expression FROM {node_revision__field_segmentid_test} t WHERE field_segmentid_test_ae_segmentid IS NOT NULL LIMIT 1 OFFSET 0; Array ( ) in Drupal\Core\Entity\Sql\SqlContentEntityStorage->countFieldData() (line 1714 of /home/avery_ae/drupal-plugin.appreciationengine.com/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).

It's also worth noting that I went into the DB and manually removed the field tables that were created to try and resolve this issue.

Please help, I am new to Drupal, and I am trying to hack and slash these fields, as I am finding it exceedingly hard to find adequate documentation and tutorials. As a side note, I've been loosely following this tutorial in order to create my custom field.

Thanks in advance!

A note on formatting

I have wrapped the error messages in code blocks, some of them are very long, so I assume that that is an acceptable format, however, if it isn't please let me know as I know it also makes it potentially a lot harder to read.

UPDATE

Removing the fields manually from the config table, has at least allowed me to uninstall the plugin.

  • would be helpful if you would share your current code somewhere. drush entup didn't help? You need to install those fields, doesn't happen automatically. You also can't just delete them by hand, because Drupal will still think they exist. I would not recommend to add additional base fields through alter hooks to other entity types like nodes, configurable fields are fine, you can also export them and add them to a module to be installed by default. Considering using a completely custom entity type instead if you want it to be definded in code – Berdir Jan 18 '18 at 20:38
  • I could post the code, but I wan't to clarify a few things first. 1. I am adding these custom fields through a Drupal module (I updated the question to clarify that). 2. I was not able to install Drush on our dreamhost server, we wen't through some harrowing trials trying to get that to work, but maybe that is not even relevant anymore given that I am working on a module. 3 I am not using any alter hooks, unless that is happening behind the scenes. 4 my field has to be pluggable on any content type, or else it defeats it's functionality. – Avery Kushner Jan 18 '18 at 23:11

Your Answer

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

Browse other questions tagged or ask your own question.