I'm using hook_form_alter to alter search block form (actualy i need to add a adv. search link. So i decidet to use markup element

<?php
    $form
['ad_search'] = array(
       
'#markup' => l('advanced search','search/node'),
       
'#weight' => 1000,
    );
?>

As mentioned in api: http://api.drupal.org/api/drupal/developer%21topics%21forms_api_referenc...

markup

Description: Generate generic markup for display inside forms. Note that there is no need to declare a form element as #type = 'markup', as this is the default type.

But search module produce a notice:

Notice: Undefined index: #type in function template_preprocess_search_block_form() (line 1070 in file /var/www/rating.local/modules/search/search.module).

I believe that line 1070

<?php
$type
= $variables['form'][$key]['#type'];
?>

must be changed to

<?php
$type
= empty($variables['form'][$key]['#type']) ? 'markup' : $variables['form'][$key]['#type'];
?>
Files: 
CommentFileSizeAuthor
#4 drupal7x-search-block-undefined-index-1928690.patch1.03 KBovi indrei
PASSED: [[SimpleTest]]: [MySQL] 40,442 pass(es).
[ View ]

Comments

Status:Active» Closed (works as designed)

Status:Closed (works as designed)» Active

#markup is a valid property of #type markup and #type item elements.

So, either http://api.drupal.org/api/drupal/developer%21topics%21forms_api_referenc... is wrong, or there's a bug in form handling, where the default #type is not assigned.

Not "should be" but "may be".

See example at that page uder markup section, not #markup:

<?php
$form
['contact_information'] = array(
 
'#markup' => variable_get('contact_form_information',
t('You can leave us a message using the contact form
below.'
)),
);
?>

When i alter add node form and adding markup fields all works without any notice:

Title:Assuming, that #type will be not empty alwaysSearch block template: Undefined index: #type
Version:7.20» 7.x-dev
Assigned:Unassigned» ovi indrei
Status:Active» Needs review
StatusFileSize
new1.03 KB
PASSED: [[SimpleTest]]: [MySQL] 40,442 pass(es).
[ View ]

Here's a quick patch that fixes the issue. Please test and confirm that is fixed. Thanks.

Works for me. But i still belive that we need to assign 'markup' insead empty string. May be sometime later it needs to add special case for 'markup' as it done to hidden and token now.

includes/common.inc 5803..5807
drupal_render()

<?php
 
// If #markup is set, ensure #type is set. This allows to specify just #markup
  // on an element without setting #type.
 
if (isset($elements['#markup']) && !isset($elements['#type'])) {
   
$elements['#type'] = 'markup';
  }
?>

The hidden and token Form API #types are both hidden form fields and this is basically the reason why they get treated different than the rest of the fields. So I really don't think the markup element will ever get treated the same as the other hidden elements.

Status:Needs review» Reviewed & tested by the community

Seems like nobody has anything else to add. Since this is a trivial fix, already confirmed to work, I'll push this issue to the next step. Feel free to intervene though, if you think it's the case.

Status:Reviewed & tested by the community» Needs review

You can't RTBC your own patches.

@marcingy, I know, I read the documentation, but I considered this to be the exception, because it's a very trivial fix.
Besides, @yark confirmed it is working. I thought things are supposed to move quicker with these quick fixes. Any feedback is welcomed.

Status:Needs review» Reviewed & tested by the community

I ran into the same issue and can confirm the error isn't showing anymore.

Had the same issue, can confirm that patch #4 resolves it without any other implications.
@ovidyou: thanks a ton!

Assigned:ovi indrei» Unassigned

patch #4 works fine for me too

Status:Reviewed & tested by the community» Fixed

Committed to 7.x - thanks! http://drupalcode.org/project/drupal.git/commit/af8d800

(And I confirmed that this doesn't appear to be an issue in Drupal 8.)

Automatically closed -- issue fixed for 2 weeks with no activity.