Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By Tregonia on
While I am working on the Search block, I would imagine that this will come into play with the pre-processing of any block.
My goal is simple; I want to turn this:
<input type="submit" id="edit-submit" name="op" value="Search" class="form-submit">
into this:
<button type="submit" id="edit-submit" name="op" class="form-submit">Submit</button>
Basically, moving the value to the correct spot and changing the element. All of the searches I have done explain how to change attributes, but not necessarily the element or moving the value for the element.
Comments
edit the tpl
If it is a search block created using Views module (views exposed filter), then the easiest way is to make a copy of the related template in your theme folder, edit it and make changes there.
I am using the standard
I am using the standard search block that comes stock with Drupal.
I have the following in as a hook, but this feels like a hack. I would love to know the correct way.
The main reason for this is to have access to CSS pseudo-elements; something that input elements do not have.
You will need to implement
You will need to implement hook_form_alter() for this. Here is the rough example. Let's say your form key is 'some_form_key' and the key for the submit button is 'submit_button':
That does not actually create
That does not actually create a button element; it creates a input element. See the code below taken directly from the browser. I added comments
Here is the hook code; i just added the bottom line today with your suggestion.
You're right, it does create
You're right, it does create an input.
One note before I get started, you don't need to set '#type' => 'item' (or anything else) when you set #markup, because the default #type is 'markup' if nothing else is set.
Anyways, since you want a <button/> tag, you can do this:
But it's not fundamentally different than what you have. You could also pass a render array set to #theme => html_tag. This will work, however I don't believe the values will be processed on the server side. To do that, you'll have to implement hook_element_info(), and the relevant callbacks from there. But that all said and done, I don't think your current method (passing the button in the #markup) will process the value on the server-side either.
I think the code you have is
I think the code you have is the better solution. I wonder if the ability to change this would ever find it's way into the API.
None-the-less, I thank you for the response.
I found I had to specify '
I found I had to specify
'#type' => 'markup'
explicitly before it worked in Drupal 7.54. Otherwise the above solution worked.This works fine, except I am
This works fine, except I am not sure how to remove the actual form-actions
<input>
. Is adding 'element-invisible' class the only option?same issue
@Ratul Saha, did you solve this? i have the same issue (submit button added, but original submit input remains). there must be a way to unset or remove the unneeded submit input, but i couldn't crack it.
Add the following code
Add the following code snippet to template.php of your theme and you can:
I was able to do this through
I was able to do this through this code: In MYTHEME template.php
function MYTHEMENAME_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'search_block_form') {
$form['actions']['submit']['#type'] = 'button';
$form['actions']['submit']['#attributes']['class'][] = 'submit_search-btn';
}
}
I also added a class because I needed it