0

I have a hook_menu as follows :

/**
 * Implementation of hook_menu().
 */
function conditional_fields_menu() {
  $items = array();

  foreach (node_get_types() as $type) {
    $content_type = content_types($type->type);

    $items['admin/content/node-type/' . $content_type['url_str'] . '/conditional'] = array(
      'title' => 'Conditional fields',
      'page callback' => 'drupal_get_form',
      'page arguments' => array('_conditional_fields_admin', $content_type['type']),
      'access arguments' => array('administer conditional fields'),
      'type' => MENU_LOCAL_TASK,
      'weight' => 5
      );
  }

  return $items;
}

How can i convert this to conditional_fields.routing.yml in d8 ?

what i tried :

Drupal 8 conditional_fields.routing.yml snippet

conditional_fields.admin:
  path: 'admin/content/node-type/{this is unknown}/conditional'
  defaults:
    _form: '_conditional_fields_admin'
    _title: 'Conditional fields'
  requirements:
    _permission: 'administer conditional fields'
1
  • What do you mean with this is unknown? What you should enter there? Have a look at node.routing.yml You can enter a variable that will be read and can be used later on in the .yml file. Apr 21, 2016 at 13:12

1 Answer 1

3

The routing system uses pattern matching. You can define a parameter in the path that will be used as a variable.

If we look at this snippet of node.routing.yml:

node.add:
  path: '/node/add/{node_type}'
  defaults:
    _controller: '\Drupal\node\Controller\NodeController::add'
    _title_callback: '\Drupal\node\Controller\NodeController::addPageTitle'
  requirements:
    _node_add_access: 'node:{node_type}'
  options:
    _node_operation_route: TRUE
    parameters:
      node_type:
        with_config_overrides: TRUE

You can see the {node_type} in the path. This variable will contain the node type as specified in the path. You can use that as part of the requirement as you can see above, and it will also be available to your functions. \Drupal\node\Controller\NodeController::add receives the node_type variable as well:

public function add(NodeTypeInterface $node_type) {
  $node = $this->entityManager()->getStorage('node')->create(array(
    'type' => $node_type->id(),
  ));

  $form = $this->entityFormBuilder()->getForm($node);

  return $form;
}

Read Parameters in routes for more information.

And for forms:

example.user_form
  path: 'example/form/{user}'
  defaults:
    _form: '\Drupal\example\Form\ExampleForm'
  requirements:
    _permission: 'access content'

You can use the buildForm function and place the additional parameters after $form and $form_state.

namespace Drupal\example\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Session\AccountInterface;

class BasicForm extends FormBase {

 public function buildForm(array $form, FormStateInterface $form_state, AccountInterface $user = NULL) {
    // Do something with $user in the form
  }
}
5
  • But node module for d8 is not available , so how can i implement this ?
    – KTM
    Apr 21, 2016 at 13:51
  • 1
    How do you mean? This is example of how the routing is done in the node module. Node module ships with D8 core so it is available. Apr 21, 2016 at 13:53
  • ok i got it from drupal/core/modules/node , thanks
    – KTM
    Apr 21, 2016 at 15:50
  • can you pls give a full replacement with respect to my code , if possible
    – KTM
    Apr 21, 2016 at 15:51
  • Everything you need to know is in my examples and the linked resources. Please read the 'parameters in routes' article I linked to. Understand it and then look at the code again. Apr 21, 2016 at 15:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.