How to place markup after the submit button in drupal 7? - drupal

goal : I want to place a text after the submit button using hook_form_alter.
Below is my code but if I use the weight key in the array then its postion does move in the form but not below the submit/save button. What can I do for it to go below the save button ?
$xdmp_twoshow="text";
$form['previewxdmp'] = array(
'#type'=>'markup',
'#markup'=>$xdmp_toshow,
'#weight' => 35,
);

Each element can have a #prefix and #suffix property which will place text/HTML immediately after the rendered output. With that in mind you can simply attach a suffix to the existing submit button like so:
$form['submit']['#suffix'] = '<p>Some text to place after the submit button</p>';
Obviously the location of the submit button within the $form array might be slightly different depending on the form you're altering.

I did it as follows:
/**
* Implements hook_form_FROM_ID_alter()
*/
function mymodule_form_mycontenttype_node_form_alter(&$form, &$form_state, $form_id) {
$form['actions']['submit']['#suffix'] = '<p>' . t('Some text to place after the submit button.') . '</p>';
}

Submit buttons are usually placed in $form['actions'] which has the default weight of 100. You can either set the weight of the $form['actions'] as you like, or give other elements a weight greater than 100 to place them after the submit button.
Example 1:
All elements with #weight greater than 34 will be placed after the submit button (and any other action buttons).
$form['actions']['#weight'] = 34;
Example 2:
Only the given element will be placed after the submit button.
$form['previewxdmp'] = array(
'#type' => 'markup',
'#markup' => $xdmp_toshow,
'#weight' => 101,
);

Related

WordPress: wp_login_form how to add class name or placeholder text

How can I add placeholder or CSS classes to login fields using wp_login_form?
The function wp_login_form in wp-includes/general-template.php renders the login form from an array of arguments:
$default = array(
'echo' => true,
...
...
);
and then creates the <form>...</form>. There is no way I can add a class name to input fields or the submit button. I want to use bootstrap classes for this purpose. Currently I have to override default classes rendered by WordPress.
I don't want to do that. For example if I want to make username box to look like an input with class form-control, I have to either write additional classes in my CSS for default WP classes or take help of jQuery to remove default classes and add mine.
What is the best way to do it? wp_login_form does not have attributes set for placeholder.
In brief I need to pass the following:
1. Pass class name from outside via an array $args,
2. Pass placeholder text for input fields
This might be a solution, eventhough it isn't perfect.
Wordpress' functon wp_login_form() does not support changing the css class. And I would recommend not to use Javascript for changing the DOM when you do not absolutely have to.
My solution to modify the CSS class was to set the echo property to false so the function returns the result as a string. Then, use str_replace() to find the classes we want to replace and replace it with the class names we want to use.
$args = array(
'echo' => false,
// etc...
And now replacing the class names...
$output = wp_login_form( $args );
$output = str_replace( 'class="input"', 'class="df-input"', $output );
echo $output;
You could also convert $output into a DOMDocument() so you can replace the class names in an more elegant manner.
According to its documentation, wp_login_form() does not receive an argument to set a class name for itself of input elements. Presumably because you wouldn't have more than one login form on the page.
It is possible to add an id to the inputs of the forms like so:
$args = array(
'form_id' => 'loginform',
'id_username' => 'user_login',
'id_password' => 'user_pass',
'id_remember' => 'rememberme',
'id_submit' => 'wp-submit',
);
However, seems that in order to set other attributes such as class and placeholders you would have to use Javascript. This can be done without JQuery. This would look like something along the lines of document.querySelector('#username').setAttribute("class", "username");
See the querySelector documentation.

Add custom style button to wysiwyg in Advanced custom fields

I would like to add a custom button to the wysiwyg editor in Advanced custom fields. I have not found any good solution on how to do it.
What I want is a button that wraps the selected text in <span class="someclass"></span> so that I can style this as I want.
How do I do this?
What you will need to do is tie into your TinyMCE editor buttons. This will add a format dropdown to your WYSIWYG editor for the class. You can then select your text and choose the class from the dropdown. Use text view to check to see if it worked.
/*
* Callback function to filter the MCE settings
*/
// Callback function to insert 'styleselect' into the $buttons array
function my_mce_buttons_2($buttons) {
array_unshift($buttons, 'styleselect');
return $buttons;
}
// Register our callback to the appropriate filter
add_filter('mce_buttons_2', 'my_mce_buttons_2');
function my_mce_before_init_insert_formats($init_array) {
// Define the style_formats array
$style_formats = array(
// Each array child is a format with it's own settings
array(
'title' => 'Some Class',
'inline' => 'span',
'classes' => 'someclass'
),
);
// Insert the array, JSON ENCODED, into 'style_formats'
$init_array['style_formats'] = json_encode($style_formats);
return $init_array;
}
// Attach callback to 'tiny_mce_before_init'
add_filter('tiny_mce_before_init', 'my_mce_before_init_insert_formats');

How to click on a button in PHPUnit (Symfony2)

Hi, I'm writing a functional test and I want to know how to perform a simple click on a button, I have a hidden form that is shown after the button click
I tried doing a normal click like that :
$button = $crawler->filter('button:contains("Add")');
$crawler = $client->click($button);
but it seems the click() function take a Link Object not a Crawler Object.
how can I do something like that ?
I assume you use JS to show your hidden form.
This will not work since the crawler doesn't support JS, you better look up CasperJs or some other crawler if you want to test the click and visibility of your form.
Symfony2 Functional Testing - Click on elements with jQuery interaction
Otherwise if testing the submit of the form is what you want to achieve, then you can use :
$form = $crawler->filter('button#idofyourbutton')->form(array(
'firstname' => 'Blabla',
'lastname' => 'Blabla',
'address' => 'BlablaBlablaBlablaBlabla',
'zipcode' => '302404',
'phone' => '30030130269'
),'POST');
$client->submit($form);
From the docs it says to convert to a link object you would do the following
$button = $crawler
->filter('button:contains("Add")') // find all buttons with the text "Add"
->eq(0) // select the first button in the list
->link() // and click it
;
And then you can click it like before..
$crawler = $client->click($button);
I haven't used this though so I'm not sure if it would work with a button.

Make changes to main form with ahah callback in Drupal?

I have a form like poll form.When there is no data in db I want to show only add button and when user clicks "more" I want to show him/her a submit button.
I used the following code but it seems doesn't works.
if ($form['count']['#value'] > 0) {
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit')
);
}
How can I do this?
The values will be in $form_state['values'] so try:
if($form_state['values']['count'] > 0){....
I would expect that at this point the $form['count']['#value'] is not set.

drupal_render: how to make well-formed div structure of the form?

I noticed, forms "story_node_form" or "node_form" are formed like this: form->div->div->div-standard+div-admin+submin-button
My custom content type is not formed this way:
I have 49 checkboxes. I placed them with drupal_render() into table like here: http://eugen.gotdns.com/test/zeitplaner.png
After that i've done drupal_render($form) to render the rest of the form
Problems:
1) My created table is on the top of the form, how can i move it down?
2) How can i place my table into collapsable group?
3) Why submit and preview buttons are on the top of the form below my table?
Because you rendered your table, then the rest of the form it is acting as expected, whereas if you where to place it in the $form array with the appropriate weight it would be rendered where it is expected with only the one drupal_render($form).
As for the collapsible group, you'd want to wrap your form elements in a collapsible fieldset, like so:
$form['wrapper-id'] = array(
'#type' => 'fieldset',
'#title' => t('title'),
'#collapsible' => TRUE,
);
$form['wrapper-id']['yourstuff'] = array(
// Your stuff
);
For more information about modifying forms, refer to the Form API documentation: http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html/6

Resources