0

How could I build a reusable form that can be reconfigured for each location in which it is used? It would have the same fields, however select list values would be different depending on what the user thinks it should contain based on its location in the site.

Locations include blocks and panels across many pages and sites, and potentially multiple locations on the same page.

1 Answer 1

0

In a custom module, use the FormAPI to build your form and create variables to store your #options for example (in the case you mentioned of select lists that change). Set the variable according to the context and you should be good to go.

/**
 * Implements hook_form();
 */
function mymodule_form($form, &$form_state) {

  // For inspecting information about entity and id:
  kpr(arg(0));
  kpr(arg(1));

  $form['selected'] = array(
    '#type' => 'select',
    '#title' => t('Selected'),
    '#options' => $variable_according_to_context,
    '#default_value' => $category['selected'],
    '#description' => t('Varies according to context.'),
  return $form;
}

The $variable_according_to_context will hold the options of your select list. You can set this variable to different options according to the context you are in. You can use arg(0) or arg(1) to get the node or entity type the form is in. I hope that was clear enough on how I would approach this.

2
  • In this case, is variable according to context referring to the site location?
    – jrounsav
    Apr 10, 2016 at 5:39
  • I've edited the answer to give more info about how to get hints on the context the form is in. Hope that helps
    – wranvaud
    Apr 12, 2016 at 22:06

Your Answer

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

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