0

Under custom_changes.routing.yml For argument {first_arg}, I am adding default value under defaults

custom_changes.argument:
  path: '/arg-demo/{first_arg}'
  defaults:
    _controller: '\Drupal\custom_changes\Controller\StaticContent:content_arg'
    _title: 'Content With Arg'
    first_arg: 100

Whereas for entity parameter as seen below

custom_changes.node_view:
  path: '/list/{node_1}/{node_2}'
  defaults:
    _title: 'List Nodes'
    _controller: '\Drupal\custom_changes\Controller\StaticContent:node_list'
    parameters:
      node_1: 52
      node_2: 52
  requirements:
    _permission: 'access node listing'
  options:
    parameters:
      node_1: 
        type: node
      node_2: 
        type: node

OR

custom_changes.node_view:
  path: '/list/{node_1}/{node_2}'
  defaults:
    _title: 'List Nodes'
    _controller: '\Drupal\custom_changes\Controller\StaticContent:node_list'
      node_1: 52
      node_2: 52
  requirements:
    _permission: 'access node listing'
  options:
    parameters:
      node_1: 
        type: node
      node_2: 
        type: node

It does not add default value if I visit /list and also gives an error

TypeError: Argument 1 passed to Drupal\custom_changes\Controller\StaticContent::node_list() must be an instance of Drupal\node\Entity\Node or null, string given in Drupal\custom_changes\Controller\StaticContent->node_list() (line 24 of modules/custom/custom_changes/src/Controller/StaticContent.php).

<?php

namespace Drupal\custom_changes\Controller;

use \Drupal\Core\Controller\ControllerBase;
use \Drupal\node\Entity\Node;

Class StaticContent extends ControllerBase  {
  public function static_callback() {
    $build = [
      '#markup' => $this->t('Hello World!'),
    ];
    return $build;
  }

  public function content_arg($first_arg) {
    $build = [
      '#markup' => $this->t('Hello World' . $first_arg),
    ];

    return $build;
  } 

  public function node_list(Node $node_1 = NULL, Node $node_2 = NULL) {
    $view_builder = \Drupal::entityTypeManager()->getViewBuilder('node');
    $storage = \Drupal::entityTypeManager()->getStorage('node');
    $view = $view_builder->viewField($node_1->get('body'), [
      'type' => 'string', // string, entity_reference_label
      'label' => 'hidden',
      'settings' => ['link' => FALSE],
      ]);
    $view_title = $view_builder->viewField($node_2->getTitle(), [
      'type' => 'string',
      'label'=> 'hidden',
      'settings' => ['link' => FALSE],
    ]);
    $output_title = render($view_title);
    $output = render($view);    
    $build = [
      '#markup' => $output + $output_title,
    ];
    return $build;
  }
}

Also should we use Node or NodeInterface and how is that determined in typehinting for method node_list under class StaticContent

  • 2
    Just change type:node to type: entity:node – MrD Jul 31 at 11:30
  • Thanks @MrD, it worked please add it as answer so its in Q & A format if possible, else I will do it – harshal Jul 31 at 11:37

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.