Rectangle 27 7

Themes for Drupal 7 form elements need to use the new render array key instead of variables in the theme definition:

function mymodule_theme() {
  return array(
    'custom_radios' => array(
      'render element' => 'element',
    ),
  );
}

Once you've made the change clear Drupal's caches and your code should work (I've just tested the above and it works fine).

Wow I did not know about this, I must go and reread their docs then. Thanks it worked.

php - Theming forms on alter with Drupal 7 - Stack Overflow

php forms drupal drupal-7 drupal-theming
Rectangle 27 0

If your theme function returns an array - you can specify which key of the array to use for rendering.

render element: The name of the renderable element or element tree to pass to the theme function. This name is used as the name of the variable that holds the renderable element or tree in preprocess and process functions.

Drupal 7 How to use render element instead of variables in hook_theme(...

drupal-7 drupal-theming
Rectangle 27 0

When using a drupal_get_form menu callback, you should return your $form array and not an already rendered themeable array. Probably you are missing #attached js files for the managed_file field.

What you could do in your case is to add the table output on a markup field of your form.

$form['upload'] = array(
    '#type' => 'managed_file',
    '#title' => t('Select a YML file'),
    '#progress_message' => t('Please wait...'),
    '#progress_indicator' => 'bar',
    '#description' => t('Click "Browse..." to select a file to upload.'),
    '#required' => TRUE,
    '#upload_validators' => array('file_validate_extensions' => array('yml txt docx')),
    '#upload_location' => $upload_dest,
);

$form['table'] = array(
    '#markup' => theme('table', array('header' => $header, 'rows' => $rows)),
);

return $form;

Sorry - I thought I deleted that comment. It wasn't even for this forum (stupid copy/paste). I have been out of the office for a couple days and haven't had a chance to try it. I'll give it a shot tomorrow.

This does work. It isn't the recommended method, which is to register your own callback and render the form and table yourself using a theme callback (see e9p.net/theming-drupal-7-form-elements-table). But this seems to be the only way that works for my use case. Thanks!

drupal - managed_file form element doesn't render correctly - Stack Ov...

drupal form-api
Rectangle 27 0

In Drupal, there is no such thing as default UL classes. There are theme functions (recognizable by the function name starting with theme_) and templates (recognizable by the filename ending with .tpl.php). Neither makes any assumptions about what HTML elements are used to render the data. Especially, Drupal makes no assumptions about which classes should be applied to the elements that a template or theme function produces.

Drupal 7 - assigning custom class to nested menu UL elements - Stack O...

drupal menu
Rectangle 27 0

I'm not sure why that's happening, there's no good reason for your element to be rendered inside the wrapper for the submit button.

An easy fix, though, would be to use the #prefix attribute on the submit button which would guarantee that your markup was rendered immediately before the wrapper for the submit button:

$form['submit']['#prefix'] = '<div>&ndash;or&ndash;</div>';

Just to address your edit, I think the same solution can apply if you set the #prefix of the distance element instead:

$form['distance']['#prefix'] = '<div>&ndash;or&ndash;</div>';

It's possible that there's some extra formatting done by another module that implements hook_form_alter which happens to run after yours, messing up your good work. By applying the prefix to the distance element you'll be ensuring it comes immediately before the field_school_name_value element.

I should mention that I've had problems with exactly this when taking a reference to an array member of the provided $form (which you're doing with $school = &$form['field_school_name_value'];). As an extra sanity check I'd recommend changing that bit of code to this and see if it helps (try this before the other suggestion above as it might just fix it):

$form['field_school_name_value']['#attributes'] = array('size' => 15);
$form['field_school_name_value']['#weight'] = 1;

instead of

$school = &$form['field_school_name_value'];
$school['#attributes'] = array(
  'size' => 15,
);
$school['#weight'] = 1;

Sorry, my original question was unclear. I want it to appear between the 'field_school_name_value' element and the 'distance' element. Seems like if I weight it between the weights of those two, it should work... but it doesn't.

@Xaq: No worries, I've updated the answer above it should help a bit.

I implemented both your suggestions, but the problem I'm having persists. I need to have the divider be on the same level as the other form elements, because it needs to be centered horizontally between the upper and lower pairs of the other elements ({state and school} and {distance and submit}. Putting the divider as a prefix puts it before the element, but still inside the wrapper. Then it's centered relative to the width of the element it prefixes, which is shorter than the width of the form as a whole.

How do I keep a Drupal form "markup" element from rendering inside the...

drupal form-api
Rectangle 27 0

arguments changed to variables in Drupal 7, and since either variables or render element is required the registry won't be picking up your theme.

Make the two changes in the hook_theme() implementation, clear the caches, and you should be good to go.

I tried this, but this did not resolve this.

What's in your adverts-single.tpl.php file?

Static html page to test, normally it has a bunch of variables which need to be filled in but its not even calling the page so..

php - Drupal 7 returning theme() to page - Stack Overflow

php drupal drupal-themes
Rectangle 27 0

I find that simply throwing away the CSRF token is not an option. We solved it using hook_theme_registry_alter() to overwrite the Drupal core theme_hidden() function so that the hidden form element 'form_token' is rendered as an <esi /> tag. The tag will cause Varnish to make a call to a PHP file which we allow to pass through the cache. This file will calculate the proper form token for the current user and will then output the HTML code for the hidden field. You can calculate this token without a Drupal bootstrap, but you will need a single DB query to fetch the *drupal_private_key* for your site, which is stored in the variable table.

Preventing form_token from rendering in Drupal "GET" forms - Stack Ove...

drupal drupal-6
Rectangle 27 0

It's true that you can set $element['#attributes']['id'] and that will apply to the form field. However, it will break labels and #states in Drupal 7 because the rest of the rendering pipeline reads the ID from somewhere else. So for your labels and #states to keep working, use set the ID to $element['#id'] instead (an undocumented property that nonetheless is how the form API watches ID internally).

Make sure to pass your ID through drupal_html_id as well to ensure no conflicts.

Drupal form with custom ID - Stack Overflow

drupal forms element drupal-fapi
Rectangle 27 0

I'm not sure why that's happening, there's no good reason for your element to be rendered inside the wrapper for the submit button.

An easy fix, though, would be to use the #prefix attribute on the submit button which would guarantee that your markup was rendered immediately before the wrapper for the submit button:

Just to address your edit, I think the same solution can apply if you set the #prefix of the distance element instead:

It's possible that there's some extra formatting done by another module that implements hook_form_alter which happens to run after yours, messing up your good work. By applying the prefix to the distance element you'll be ensuring it comes immediately before the field_school_name_value element.

I should mention that I've had problems with exactly this when taking a reference to an array member of the provided $form (which you're doing with $school = &$form['field_school_name_value'];). As an extra sanity check I'd recommend changing that bit of code to this and see if it helps (try this before the other suggestion above as it might just fix it):

instead of

Sorry, my original question was unclear. I want it to appear between the 'field_school_name_value' element and the 'distance' element. Seems like if I weight it between the weights of those two, it should work... but it doesn't.

@Xaq: No worries, I've updated the answer above it should help a bit.

I implemented both your suggestions, but the problem I'm having persists. I need to have the divider be on the same level as the other form elements, because it needs to be centered horizontally between the upper and lower pairs of the other elements ({state and school} and {distance and submit}. Putting the divider as a prefix puts it before the element, but still inside the wrapper. Then it's centered relative to the width of the element it prefixes, which is shorter than the width of the form as a whole.

How do I keep a Drupal form "markup" element from rendering inside the...

drupal form-api
Rectangle 27 0

function MYTHEMENAME_search_theme_form($form) {
  return "<h1>ceci nes't pas un form</h1>";
}

Then refresh your theme registry and you should see your h1-line appearing on the place of the form.

I don't have problem with hook_theme being in template cuz it is used and my template receive $form array as well. But, when I try to render each element drupal_render, it doesn't show anything. I've checked and $form contains all the elements I needed. Right now, I have no choice but to use $search which contains rendered elements enclosed in tags created by search module itself.

No. You do have a problem with hook_theme in template.php, cuz your solution does not work. As per you question :)

drupal - drupal_render doesn't render form element - Stack Overflow

drupal search forms drupal-render
Rectangle 27 0

In the example below, I created a new theme function using a render element.

/**
 * Implements hook_theme().
 */
function rojo_theme() {
  $items = array(
    'rojo_content' => array(
      'render element' => 'element',
    ),
  );
  return $items;
}

/**
 * Theme function.
 */
function theme_rojo_content($vars) {
  return '<pre>' . print_r($vars, TRUE) . '</pre>';
}

/**
 * Render function.
 */
function rojo_render() {
  $build = array(
    '#theme' => 'rojo_content',
    '#module' => 'rojo',
    'content' => 'Page content for the render function',
    'list' => array('one', 'two'),
    'tag' => 'div',
  );
  return render($build);
}

This will print the output of the $vars passed into the theme function. From here, you should be able to see what is going on. The #theme property will be called with theme() and passed the $build array during the render() process. Notice I added #module property and Drupal added the #printed / #children properties.

This is purely an example to demonstrate the creation of a new theme function using render element and argument passing. I hope this helps somebody out.

Array
(
  [element] => Array
    (
      [#theme] => rojo_content
      [#module] => rojo
      [content] => Page content for the render function
      [list] => Array
        (
          [0] => one
          [1] => two
        )
      [tag] => div
      [#printed] => 
      [#children] => 
    )
)

Drupal 7 How to use render element instead of variables in hook_theme(...

drupal-7 drupal-theming
Rectangle 27 0

I found the answer to my own question. The important thing to understand is that there are 2 ways that links can be rendered in Drupal 7. The first is using the old l() function and the other is using a render array. Many of the theme functions in Drupal use the old style and when it's used it is not run through the drupal_prerender_link(). Which is what does the element pre-processing that communicates the settings from php to the js layer.

How do you remove the throbber from drupal 7 Ajax link? - Stack Overfl...

ajax drupal drupal-7
Rectangle 27 0

All theme functions in Drupal 7 take a single array argument (usually named as $vars or $variables by convention), and that array contains the variables/render elements you've declared. The theme function itself would look like this:

function theme_agil_list_form($vars) {
  $form = $vars['form'];
  // Now manipulate $form
}

Also you need to tell Drupal that your form will be using this theme, by doing this in your form function:

$form['#theme'] = 'agil_list_form';

Great. It works. I missed the $form['#theme'] in my form declaration and it was not mentionned in the documentation I read... Thx !

Calling custom Theme() function for form in Drupal 7 - Stack Overflow

drupal themes hook
Rectangle 27 0

Themes for Drupal 7 form elements need to use the new render array key instead of variables in the theme definition:

function mymodule_theme() {
  return array(
    'custom_radios' => array(
      'render element' => 'element',
    ),
  );
}

Once you've made the change clear Drupal's caches and your code should work (I've just tested the above and it works fine).

Wow I did not know about this, I must go and reread their docs then. Thanks it worked.

php - Theming forms on alter with Drupal 7 - Stack Overflow

php forms drupal drupal-7 drupal-theming
Rectangle 27 -1

render() is defined as render(&$element). As explained on Passing by Reference, when a function needs a reference as parameter, you can pass to the function:

render($a)
render(new ElementArray())
render(elements_to_render())
elements_to_render()
function &elements_to_render()

To notice that, in the first case, passing $a is different from passing $page['header'].

function test_ref(&$myvar){   return 'value: '.$myvar; }  $myarray = array(1, 2, 3);  print test_ref($myarray[0]);
value 1

(sorry for the horrible formatting, line breaks are not allowed in comments)

php - Drupal 7 custom template error: Only variables can be passed by ...

php templates drupal drupal-7 drupal-theming