0

Introduction (not necessarily needed to be able to answer the question)

I use Domain Access module to be able to manage different sites from one Drupal installation and a single shared database. I enabled Domain Content submodule that provides a content administration page for each subdomain.
Instead of having all contents of all domains displayed at /admin/content, this allows to display all contents of a given domain at /admin/content/domain-content/mydomain.

What I'd like to do

I'd like to prefill a form element in Create Article page according to a given domain.

So I plan to add a Add content button at /admin/content/domain-content/mydomain, and pass the parameter mydomain from here until, for example, Create Article page.

Then I'll be able to prefill (tick) the right domain checkbox in the Create Article page according the passed argument.

My question

How to pass parameters between the given admin pages to be able to prefill a form in Create Article page?

1 Answer 1

2

Altering the form

You should implement the hook_form_FORM_ID_alter. Then, read the parameters like this:

$name = \Drupal::request()->request->get('country'); // from param

Then alter the form elements, rewriting the #defatult_value conditionally, like this:

$form['field_country']'#default_value'] = 'my domain specific default';

In the case of a checkbox widget, the #default_value is an array (source):

$form['countries']['#default_value'] = [1, 3];

Passing parameters in general

If your parameters are simple strings, like country: denmark, you could use GET parameters. This answer has the sample code:

How do I create a link?

Passing parameters through the '/node/add' route

This is a special route, it is not so simple.

Method 1: Implementing a subtheme

If you are using the default admin theme ('seven') you would have to create a subtheme, and overwrite this hook, so that your custom parameters are passed:

   /**
     * Implements hook_preprocess_HOOK() for list of available node type templates.
     */
    function seven_preprocess_node_add_list(&$variables) {
      if (!empty($variables['content'])) {
        /** @var \Drupal\node\NodeTypeInterface $type */
        foreach ($variables['content'] as $type) {
          $variables['types'][$type->id()]['label'] = $type->label();
          $variables['types'][$type->id()]['url'] = \Drupal::url('node.add', ['node_type' => $type->id()]);
        }
      }
    }

Method 2: Overwriting the twig template

The links are ultimately rendered by the core/themes/seven/templates/node-add-list.html.twig template. To overwrite this output, you would: - Implement a hook_theme_suggestions_HOOK - Copy the node-add-list.html.twig to node-add-list--custom.html.twig and implement your custom changes here.

/**
 * Implements hook_theme_suggestions_HOOK()
 */
function hook_theme_suggestions_node_add_list(array $variables) {
  $suggestions = array();
  $suggestions[] = 'node_add_list__custom';
  return $suggestions;
} 
3
  • Since I need to pass dynamic arguments, I edited my question to be more precise.
    – Kwadz
    Sep 7, 2018 at 14:11
  • Thanks, that's a good idea. I'd like to mention that there is an intermediate page between /admin/content/domain-content/mydomain and Create Article page. How to pass the param through the intermediate page, which is /node/add, until Create Article page?
    – Kwadz
    Sep 7, 2018 at 15:56
  • Just added the fun part: "Passing parameters through the '/node/add' route", and then reordered the sections. Sep 7, 2018 at 17:14

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.