Score:1

How do I upcast route parameters for entity_revision?

pl flag

My custom entity is revisionable. I can see revisions, but my revision revert form doesn't work. The route for the form is definited in msul_rovi_games.routing.yml.

entity.rovi_game.revision_revert_confirm:
  path: '/rovi_game/{rovi_game}/revisions/{rovi_game_revision}/revert'
  defaults:
    _title: 'Revert to an earlier revision'
    _form: '\Drupal\msul_rovi_games\Form\RoviGameRevisionRevertForm'
  requirements:
    _permission: 'administer rovi_game entities'
    rovi_game: \d+
  options:
    _node_operation_route: TRUE
    rovi_game:
      type: entity:rovi_game
    rovi_game_revision:
      type: entity_revision:rovi_game

The form is defined in src/Form/RoviGameRevisionRevertForm.php.

namespace Drupal\msul_rovi_games\Form;

use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\msul_rovi_games\Entity\RoviGameInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a form for reverting a rovi_game revision.
 *
 * @internal
 */
class RoviGameRevisionRevertForm extends ConfirmFormBase {

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, RoviGameInterface $rovi_game_revision = NULL) {
    $this->revision = $rovi_game_revision;
    $form = parent::buildForm($form, $form_state);

    return $form;
  }
}

When I attempt to revert, I get the following error.

TypeError: Argument 3 passed to Drupal\msul_rovi_games\Form\RoviGameRevisionRevertForm::buildForm() must implement interface Drupal\msul_rovi_games\Entity\RoviGameInterface or be null, string given in Drupal\msul_rovi_games\Form\RoviGameRevisionRevertForm->buildForm() (line 116 of /opt/drupal/web/modules/custom/msul_rovi_games/src/Form/RoviGameRevisionRevertForm.php)

What am I missing?

Score:4
in flag

You are just missing a level, 'parameters', in your options array. See Upcasting route slug name options. Note that parameters must be nested under options > parameters.

foobar.view:
  path: '/foobar/{foobar_placeholder}'
  defaults:
    _controller: '\Drupal\foobar\Controller\Foobar::content'
    _title: 'Oh yeah foobar'
  options:
    parameters:
      foobar_placeholder:
        type: entity:foobar

Your code updated:

entity.rovi_game.revision_revert_confirm:
  path: '/rovi_game/{rovi_game}/revisions/{rovi_game_revision}/revert'
  defaults:
    _title: 'Revert to an earlier revision'
    _form: '\Drupal\msul_rovi_games\Form\RoviGameRevisionRevertForm'
  requirements:
    _permission: 'administer rovi_game entities'
    rovi_game: \d+
  options:
    _node_operation_route: TRUE
    # Nest parameters under "parameters"
    parameters:
      rovi_game:
        type: entity:rovi_game
      rovi_game_revision:
        type: entity_revision:rovi_game
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.