Creating an instance of custom entity with entity reference field breaks entity collection page

I created two custom entities, entity_one and entity_two. entity_one has a entity reference field that allows you to select unlimited instances of entity_twos. The edit form for entity_one seems to work fine. The field exists and properly auto populates with entity_twos. However as soon as I submit and create an entity_one the list page for entity_ones gives me the error.

Error: Call to a member function getValue() on string in DrupalCoreEntityContentEntityBase->__sleep() (line 524 of core/lib/Drupal/Core/Entity/ContentEntityBase.php).

If I remove that particular field and just have textfields I do not encounter this behavior. It only occurs when I have the entity reference field. My first instinct was maybe in my EntityOneListBuilder.php it uses a field in the table that isn’t properly formatted but the only fields used are ID and title.

Here is a portion of my EntityOneListBuilder

public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {     return new static(       $entity_type,       $container->get('entity_type.manager')->getStorage($entity_type->id()),       $container->get('url_generator')     );   }    public function render() {     $build['description'] = [       '#markup' => $this->t('Description Text'),     ];     $build['table'] = parent::render();     return $build;   }    public function buildHeader() {     $header['id'] = $this->t('ID');     $header['title'] = $this->t('Title');     return $header + parent::buildHeader();   }       public function buildRow(EntityInterface $entity) {     $row['id'] = $entity->id();     $row['title'] = $entity->toLink()->toString();     return $row + parent::buildRow($entity);   } 

And here is how I’m building the entity reference field for entity_one

    $fields['entity_twos'] = BaseFieldDefinition::create('entity_reference')             ->setLabel(t('Form Fields'))             ->setDescription(t('The fields to add to this form.'))             ->setRevisionable(TRUE)             ->setSetting('target_type', 'entity_two')             ->setTargetEntityTypeId('entity_two')             ->setSetting('handler','default')             ->setDisplayOptions('view', [                 'label' => 'hidden',                 'type' => 'entity_two',                 'weight' => 0,             ])             ->setDisplayOptions('form', [                 'type' => 'entity_reference_autocomplete',                 'weight' => 5,                 'settings' => [                     'autocomplete_type' => 'tags',                     'size' => '60',                     'match_operator' => 'CONTAINS',                     'placeholder' => '',                 ],             ])             ->setDisplayConfigurable('form', true)             ->setDisplayConfigurable('view', true)             ->setRequired(TRUE)             ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED); 

Why would that particular field break my collection page?

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Creating an instance of custom entity with entity reference field breaks entity collection page

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.