I'm not the world's most experienced Drupal and I'm having a nightmare trying to customise the search box. I'm using the example from the Drupal help site and it's simply not working.
I've copied search-theme-form.tpl.php from the search module into my theme and copied the example code, Any HTML in that code outside of the pre-process function shows fine but as far as I can tell, the pre-process function either isn't called or just isn't affecting my search box.
I'm almost certain that I'm missing something absolutely fundamentally basic about how Drupal works but I can't find any info at all.
Here's the code:
<?php
function danland_preprocess_search_theme_form(&$vars, $hook) {
// Remove the "Search this site" label from the form.
$vars['form']['search_theme_form']['#title'] = t('');
// Set a default value for text inside the search box field.
$vars['form']['search_theme_form']['#value'] = t('Search this Site');
// Add a custom class and placeholder text to the search box.
$vars['form']['search_theme_form']['#attributes'] = array('class' => 'NormalTextBox txtSearch', 'onblur' => "if (this.value == '') {this.value = '".$vars['form']['search_theme_form']['#value']."';} ;", 'onfocus' => "if (this.value == '".$vars['form']['search_theme_form']['#value']."') {this.value = '';} ;" );
// Change the text on the submit button
//$vars['form']['submit']['#value'] = t('Go');
// Rebuild the rendered version (search form only, rest remains unchanged)
unset($vars['form']['search_theme_form']['#printed']);
$vars['search']['search_theme_form'] = drupal_render($vars['form']['search_theme_form']);
$vars['form']['submit']['#type'] = 'image_button';
$vars['form']['submit']['#src'] = path_to_theme() . '/images/search.jpg';
// Rebuild the rendered version (submit button, rest remains unchanged)
unset($vars['form']['submit']['#printed']);
$vars['search']['submit'] = drupal_render($vars['form']['submit']);
// Collect all form elements to make it easier to print the whole form.
$vars['search_form'] = implode($vars['search']);
}
?>
<div id="search" class="container-inline">
<?php print $search_form; print $search_theme_form; ?>
</div>
Put the preprocess function in template.php in your theme directory, then clear the cache at "admin/settings/performance"
Here are some helpful links to learn about theming with templates/preprocess functions:
About overriding themable output
Setting up variables for use in a template (preprocess and process functions)
Related
I have the following input field:
{assign var="name" value="test12345.example.com"}
<input name="domain" value="{$domain}" size="50"
data-hostname-check="{$options}"
placeholder="<?php echo substr(md5(mt_rand()), 0, 7); ?>"
onchange="if (
typeof simulateCart == 'function' && this.checkValidity())
simulateCart.call(this);"/>
What i need is to generate a random string for if the input field when its not filled. But I have no clue how to do this. This input field is in an if loop using smarty template.
The onchange is required at the moment so if I remove this you can leave the input field blank but there should be a label before we can insert it into the database
At the moment I have a var (static) whichh i can use for the placeholder text. The echo which i tried does not work. Maybe someone can help me fixing this. If i put this in the placeholder the whole text in the "" will be displayed.
Another issue is that the form has an input validation and you have to press on a button. I know there is an jQuery on.click how can i use this in my situation.
You can write smarty plugin wich will output random string.
See chapter "function plugin with output" here https://www.smarty.net/docs/en/plugins.functions.tpl
You need only create file with you functuion
function smarty_function_randomstring($params, Smarty_Internal_Template $template) {
return substr(md5(mt_rand()), 0, 7);
}
Ant put it in plugin directory https://www.smarty.net/docs/en/variable.plugins.dir.tpl
Then you can use it in smarty like this
<input placeholder="{randomstring}"
I am very new in php. I have a site with the CMS joomla 3.6.
In the register of a third part component I have a select box to the members inform his gender. In the html of the page, returns the text 'male' and 'female'.
What I need is to replace the text 'male' and 'female' with images.
In the PHP file I have this:
public function getFieldData($field) {
$options = array("COM_COMMUNITY_MALE" => "COM_COMMUNITY_MALE", "COM_COMMUNITY_FEMALE" => "COM_COMMUNITY_FEMALE");
$value = strtoupper($field['value']);
if ( isset($options[$value])) {
return JText::_($options[$value]);
}else {
return '';
}
}
So, I know that the second "COM_COMMUNITY_MALE" and "COM_COMMUNITY_FEMALE" are the output html and I have tried to replace them with that code <img src="image/image.png"/>. But no luck, can someone tell me what is the path to do this?
Thank you in advance for your attention.
The problem is that you are using a dropdown box, and, for example, COM_COMMUNITY_MALE is the value and the label of one of the options in that dropdown box, and you can't have a value in a dropdown that is HTML code (for example, an img tag). You will need to review your requirements.
I'm working in PHP to build a form. I know how to display the form and then take submitted values from the $_POST variable, and I know how to validate those variables and display a "Thank You" or an "Error" page depending on the input.
What I don't know how to do, though, is create a client-side-like system wherein despite having my users hit a "back" button a separate screen I can then take the information I gathered from the first submission and display dynamic error messages like "Please provide a valid email address" or "First name is a required field" next to the fields that were entered incorrectly. I'd also like to retrieve any previously submitted data that was valid and have it populate in the form so users don't get frustrated by losing everything they entered.
What is the right approach to accomplishing something like this in PHP? I originally thought if I could pass back an array of error messages with an input type="hidden" tag I could then pull my values and display messages dynamically with PHP, but I keep getting stuck in that approach.
You could add the errors a php session, but this creates issues for users who have multiple browser tabs open.
My preferred method is to have the form submit to the same page and put the errors directly on that page so the user does not have to click the back button. That way you can highlight the fields directly in the form (make the background or outline red or something similar.)
<input type="text"
<?php (empty($_POST['field']?'style="backgroung-color: red;"':''))?>
name="field" value="<?php echo $_POST['field']?>" />
You can put <input type="text" name="field" value="<?php echo $_POST['field']?>" /> to get the old value.
Because the web is, by definition, stateless, there is no really good way to track what the user does when they hit the back button. There are hacks that work using a hidden iframe, but that is way more trouble that what you are looking for.
Don't mix client logic with server logic. The exact same script can output the form and take it's input. In case input successfully validates, it goes on. If not, it will display the form again, this time with error messages and the already-entered data.
Next time the user submits the form, validation starts again until it passes successfully.
So you extend the form with input values and error messages in the first place, but you only display them if flagged/set.
This can be done just with additional variables next to $_POST - or if you like it - by using a complete form abstraction from a framework, like zend framework (which might be overhead for what you like to do) or just with a library/component like the popular HTML_QuickForm2.
Edit:
This is some very bare code to demonstrate the overall methodology, if you use a library it is much nicer (and you don't have to code it instead you can concentrate on the actual form like the definition on top). This code is more for reading and understanding the flow than for using, I quickly typed it so it (most certainly has) syntax errors and it's not feature complete for a full blown form. This one has only one email field and is even missing the submit button:
/* setup the request */
$request->isSubmit = isset($_POST['submit']);
/* define the form */
$form->fields = array
(
'email' => array
(
'validate' => function($value) {return filter_var($value, FILTER_VALIDATE_EMAIL);},
'output' => function($value, $name) {return sprintf('<input type="text" value="%s" id="%s">', htmlspecialchars($value), htmlspecialchars($name)},
'default' => 'info#example.com',
),
);
/**
* Import form data from post request
*
* #return array data with keys as field names and values as the input strings
* or default form values.
*/
function get_form_post_data($form, $request)
{
$data = array();
foreach($form->fields as $name => $field)
{
$data[$name] = $field->default;
if ($request->isSubmit && isset($_POST[$name]))
{
$data[$name] = $_POST[$name];
}
}
return $data;
}
/**
* Validate form data
*/
function validate_form_data($form, $data)
{
foreach($form->fields as $name => $field)
{
$value = $data[$name];
$valid = $field['validate']($value);
if (!$valid)
{
$form->errors[$name] = true;
}
}
}
function display_form($form, $data)
{
foreach($form->fields as $name => $field)
{
$value = isset($data[$name]) ? $data[$name] : '';
$hasError = isset($form->errors[$name]);
$input = $field['output']($name, $value);
$mask = '%s';
if ($hasError)
{
$mask = '<div class="error"><div class="message">Please Check:</div>%s</div>';
}
printf($mask, $input);
}
}
// give it a run:
# populate form with default values -or- with submitted values:
$form->data = get_form_post_data($form, $request);
# validate form if there is actually a submit:
if ($request->isSubmit)
{
validate_form_data($form, $form->data);
}
# finally display the form (that can be within your HTML/template part), works like echo:
display_form($form, $form->data)
Use the form to submit to the same page, and if the form validates, use a header to redirect the user into the thank you page.
header("Location: thank-you.php");
If the form fails validation, you could easily display all the errors on the same page.
I'm trying to add custom user fields to the Web Users Manager area in MODx Evolution 1.0 via a custom plugin. I'm in the early stages and can't get the 'OnWUsrFormRender' hook to do anything. Any ideas?
Here's my test plugin code:
<?php
$e = &$modx->Event;
switch($e->name){
case "OnWUsrFormRender":
$fields = '
Test Info: <input type="text" name="test"/>
';
$e->output($fields); //this doesn't show up
echo 'testing'; //this doesn't show up
break;
}
?>
(PS- I've tried WebUserPE and PPP, but neither of them are good for my situation...)
You must not include
<?php ?>
tags in plugins besides that the code works fine.
Another question about Drupal webforms --
The form itself is built in by /includes/form.inc's
function theme_form_element($element, $value)
and adds a <label> element to the $output. I want to remove that label only for one webform, so I have to override the function.
How can I override it for only one webform, while leaving it the same in all others?
E.g.
if ($block == 'contact'):
// only output <input> form element stored in $value
function mytheme_html_form_element($element, $value) {
$t = get_t();
$output .= " $value\n";
return $output;
}
endif;
Is this possible, and what goes in the if condition?
If you're just looking to remove the label, you can also use hook_form_alter(), and check that $form_id is equal to the webform in question. The id will be of the form: webform_client_form_N where N is the node ID of the webform.
Once you're operating on the proper form, you can unset the label using, for example, code like this:
unset($form['submitted']['first_name']['#title']);
Which would unset the label for a field called first_name.
i did have to do a hook_form_alter, but the label itself was in the ['submitted'] element.
here is the code
if($form_id == 'webform_client_form_18') {
$form['submitted']['#children'] = '
<input
type="text"
maxlength="128"
name="submitted[email]"
id="edit-submitted-email"
value="' . $form['submitted']['email']['#default_value']. '"
class="form-text required"
/>
';
}
in a different form, removing the #title worked (+1 for you!), but this was a different case.
I wouldn't unset form element titles. You could get unexpected results when your form gets rendered by the theme engine.
You can do it several ways:
Theme each element or the whole form with with '#theme' => 'my_callback'.
You can also create your own form element using hook_elements that uses a corresponding theme hook.
See:
http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html
http://api.drupal.org/api/function/hook_elements/6