Background:

I'm working on creating a custom slider module for drupal 10. This module creates two separate entity types; "slider" and "slider_image". The slider_image entity type has an entity reference field to a specific slider entity. I'm taking inspiration from the way menus are structured in the structure admin menu, where there is a list of slider entities under home>administration>structure>sliders. Here there is a button to add a slider. When editing a given slider, there is a button to view the slider images associated with that slider. This loads a list of slider images as well as a button to add a slider image. The slider images should be able to be re-arranged similar to menu items in a menu.I will later figure out how to take these entities and create an actual placeable slider block, but I'm not there yet.

What's working:

Under Home>Administration>Structure, I have an item called Sliders.

Clicking on Sliders goes to a list of slider entities. I have one created here.

Clicking on Add Slider loads this form. Show Dots will be used to create dots underneath the slider for slide selection.

If I edit an existing slider, it loads this form. The url for this for looks like this:
sitename/admin/structure/slider/{sliderentityid}/edit?destination=/sitename/admin/structure/sliders

When clicking on the "View Slider Images" button, it loads the list of slider images for that slider. The url for this list is this:
sitename/admin/structure/sliders/{sliderentityid}/edit/images
When there are no slider images for that slider, the list looks like this:

I was going to show what the form looks like to add a slider image next, but as I was walking through this process, I ran into this error after clicking on "Add Slider Image":

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

Error: Object of class Drupal\center_slider\Entity\Slider could not be converted to string in Drupal\Core\Entity\EntityStorageBase->buildCacheId() (line 142 of core/lib/Drupal/Core/Entity/EntityStorageBase.php).

Drupal\Core\Entity\EntityStorageBase->getFromStaticCache() (Line: 282)
Drupal\Core\Entity\EntityStorageBase->loadMultiple() (Line: 46)
Drupal\Core\Field\EntityReferenceFieldItemList->referencedEntities() (Line: 98)
Drupal\Core\Field\Plugin\Field\FieldWidget\EntityReferenceAutocompleteWidget->formElement() (Line: 340)
Drupal\Core\Field\WidgetBase->formSingleElement() (Line: 207)
Drupal\Core\Field\WidgetBase->formMultipleElements() (Line: 111)
Drupal\Core\Field\WidgetBase->form() (Line: 183)
Drupal\Core\Entity\Entity\EntityFormDisplay->buildForm() (Line: 121)
Drupal\Core\Entity\ContentEntityForm->form() (Line: 107)
Drupal\Core\Entity\EntityForm->buildForm() (Line: 21)
Drupal\center_slider\Form\SliderImageForm->buildForm()
call_user_func_array() (Line: 536)
Drupal\Core\Form\FormBuilder->retrieveForm() (Line: 283)
Drupal\Core\Form\FormBuilder->buildForm() (Line: 73)
Drupal\Core\Controller\FormController->getContentResult() (Line: 39)
Drupal\layout_builder\Controller\LayoutBuilderHtmlEntityFormController->getContentResult()
call_user_func_array() (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 592)
Drupal\Core\Render\Renderer->executeInRenderContext() (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext() (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 182)
Symfony\Component\HttpKernel\HttpKernel->handleRaw() (Line: 76)
Symfony\Component\HttpKernel\HttpKernel->handle() (Line: 58)
Drupal\Core\StackMiddleware\Session->handle() (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle() (Line: 106)
Drupal\page_cache\StackMiddleware\PageCache->pass() (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache->handle() (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle() (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle() (Line: 51)
Drupal\Core\StackMiddleware\StackedHttpKernel->handle() (Line: 704)
Drupal\Core\DrupalKernel->handle() (Line: 19)

This is a new error to me now. The error I was getting was when I would click on "View Slider Images" on a slider that had slider images associated with it, I would get this error:

The website encountered an unexpected error. Please try again later. Symfony\Component\Routing\Exception\MissingMandatoryParametersException: Some mandatory parameters are missing ("slider") to generate a URL for route "entity.slider_image.edit_form". in Drupal\Core\Routing\UrlGenerator->doGenerate() (line 187 of core/lib/Drupal/Core/Routing/UrlGenerator.php).
Drupal\Core\Routing\UrlGenerator->getInternalPathFromRoute() (Line: 300)
Drupal\Core\Routing\UrlGenerator->generateFromRoute() (Line: 108)
Drupal\Core\Render\MetadataBubblingUrlGenerator->generateFromRoute() (Line: 765)
Drupal\Core\Url->toString() (Line: 183)
Drupal\Core\Utility\LinkGenerator->generate() (Line: 95)
Drupal\Core\Render\Element\Link::preRenderLink() call_user_func_array() (Line: 111)
Drupal\Core\Render\Renderer->doTrustedCallback() (Line: 797)
Drupal\Core\Render\Renderer->doCallback() (Line: 386)
Drupal\Core\Render\Renderer->doRender() (Line: 204)
Drupal\Core\Render\Renderer->render() (Line: 474)
Drupal\Core\Template\TwigExtension->escapeFilter() (Line: 77)
__TwigTemplate_1251d3c40bf9b47fa0fc0bee8e3c9500->doDisplay() (Line: 394)
Twig\Template->displayWithErrorHandling() (Line: 367)
Twig\Template->display() (Line: 379)
Twig\Template->render() (Line: 40)
Twig\TemplateWrapper->render() (Line: 53)
twig_render_template() (Line: 372)
Drupal\Core\Theme\ThemeManager->render() (Line: 445)
Drupal\Core\Render\Renderer->doRender() (Line: 204)
Drupal\Core\Render\Renderer->render() (Line: 474)
Drupal\Core\Template\TwigExtension->escapeFilter() (Line: 182)
__TwigTemplate_e1e824515bbcb2480f15d6859930ddfe->doDisplay() (Line: 394)
Twig\Template->displayWithErrorHandling() (Line: 367)
Twig\Template->display() (Line: 379)
Twig\Template->render() (Line: 40)
Twig\TemplateWrapper->render() (Line: 53)
twig_render_template() (Line: 372)
Drupal\Core\Theme\ThemeManager->render() (Line: 445)
Drupal\Core\Render\Renderer->doRender() (Line: 458)
Drupal\Core\Render\Renderer->doRender() (Line: 204)
Drupal\Core\Render\Renderer->render() (Line: 238)
Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 592)
Drupal\Core\Render\Renderer->executeInRenderContext() (Line: 239)
Drupal\Core\Render\MainContent\HtmlRenderer->prepare() (Line: 128)
Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse() (Line: 90)
Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray() call_user_func() (Line: 111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch() (Line: 187)
Symfony\Component\HttpKernel\HttpKernel->handleRaw() (Line: 76)
Symfony\Component\HttpKernel\HttpKernel->handle() (Line: 58)
Drupal\Core\StackMiddleware\Session->handle() (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle() (Line: 106)
Drupal\page_cache\StackMiddleware\PageCache->pass() (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache->handle() (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle() (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle() (Line: 51)
Drupal\Core\StackMiddleware\StackedHttpKernel->handle() (Line: 704)
Drupal\Core\DrupalKernel->handle() (Line: 19)

The issue seemed to be with how I was adding parameters to the path for the route. I'm not sure why I got the error regarding the slider_image.edit_form when loading the slider_image.list route, but that's when it would appear. I'm not sure how to added files to this post, but I can share the contents of the module's files. I'm testing this module on a site running drupal 10.1.4 at the moment. I also tried pasting photos to this post, but I haven't found where to upload a photo to drupal since it says it needs to be hosted on drupal.org

Comments

ryan_champion’s picture

I resolved the first error I was getting. In my center_slider.routing.yml file, this was the route that the "Add Slider Image" button pointed to:

entity.slider_image.add_form:
  path: '/admin/structure/sliders/{slider}/edit/images/add'
  defaults:
    # Calls the form.add controller, defined in the slider_image entity.
    _entity_form: slider_image.default
    _title: 'Add Slider Image'
  requirements:
    # Use the entity's access controller. _entity_create_access tells the router
    # to use the access controller's checkCreateAccess() method instead of
    # checkAccess().
    _entity_create_access: 'slider_image'
  options:
    parameters:
      slider:
        type: entity:slider

Once I removed the options: section of this route, the form loaded successfully. I was trying to add the parameter to the route. I'm not sure why doing it that way didn't work. As soon as I create a slider image with the add form, I get the second error in my original post.

I get that same error when I slick on the "View Slider Images" button on the Edit form of the slider. That button points to this route:

entity.slider_image.list:
  path: '/admin/structure/sliders/{slider}/edit/images'
  defaults:
  # Calls the list controller, defined in the annotation of the slider_image entity.
    _entity_list: 'slider_image'
    _title: 'Slider Images'
  requirements:
  # Checks for permission directly.
    _permission: 'view slider_image entity'
  options:
    parameters:
      slider:
        type: entity:slider

I get the same error even if I remove the options: section from this route as well.

In the error itself it mentions the route, "entity.slider_image.edit_form". I'm not sure why it's coming up with that route when the route being loaded at that point should be "entity.slider_image.list", but here is the edit_form route:

entity.slider_image.edit_form:
  path: '/admin/structure/sliders/{slider}/edit/images/{slider_image}/edit'
  defaults:
  # Calls the form.edit controller, defined in the slider_image entity.
    _entity_form: slider_image.default
    _title: 'Edit Slider Image'
  requirements:
    # Calls the access controller of the entity, passing in the suffix
    # ('update') as the $operation parameter to checkAccess().
    _entity_access: 'slider_image.update'
  options:
    parameters:
      slider:
        type: entity:slider
      slider_image:
        type: entity:slider_image

Any guidance here would be greatly appreciated.