Adding attribute to option element using forms api : drupal 7 - drupal

I want to add title="icons/icon_cart.gif" for each of the below options in my select list which is rendered using views.
After trying and reading many articles I can't seem to find the way to add this html into my form.
Below is my code.
function customchatter_form_alter(&$form, &$form_state, $form_id) {
$form["tid"]["#options"][1]=t("nooo chatter");
// this works to change the label of the option but how do I add title="icons/icon-
cart.gif" ?
}
My html code:
<select id="edit-tid" name="tid" class="form-select">
<option value="All">- Any -</option>
<option value="1">nooo chatter</option>
<option value="2">Complaints Complaints</option>
<option value="3">Gear & Gadgets</option>
</select>
Cheers,
Vishal
UPDATE
I tried to update the code as per Clive's advice but the values are still not coming up right. Below is my story.
So below is the html output I am able to achieve but the title always seems to be the number 1.
<select id="edit-select" class="form-select" name="select">
<option value="1" title="1">One</option>
<option value="2" title="1">Two</option>
</select>
As you can see title is there but the value is wrong. Below is my form and the functions which I wrote.
My form:
$form['select'] = array(
'#type' => 'select',
'#options' => array(1 => 'One', 2 => 'Two'),
'#title' => array(1 => 'One', 2 => 'Two')
// I tried many combinations but nothing seems to work.
);
My Theme functions.
function kt_vusers_select($variables) {
$element = $variables['element'];
element_set_attributes($element, array('id', 'name', 'size'));
_form_set_class($element, array('form-select'));
return '<select' . drupal_attributes($element['#attributes']) . '>' .
kt_vusers_form_select_options($element) . '</select>';
}
function kt_vusers_form_select_options($element, $choices = NULL) {
if (!isset($choices)) {
$choices = $element['#options'];
}
// array_key_exists() accommodates the rare event where $element['#value'] is NULL.
// isset() fails in this situation.
$value_valid = isset($element['#value']) || array_key_exists('#value', $element);
// #vishal so there I have declared the variable to accept the values.
$vtitle = isset($element['#title']) || array_key_exists('#title', $element);
$value_is_array = $value_valid && is_array($element['#value']);
$options = '';
foreach ($choices as $key => $choice) {
if (is_array($choice)) {
$options .= '<optgroup label="' . $key . '">';
$options .= form_select_options($element, $choice);
$options .= '</optgroup>';
}
elseif (is_object($choice)) {
$options .= form_select_options($element, $choice->option);
}
else {
$key = (string) $key;
if ($value_valid && (!$value_is_array && (string) $element['#value'] === $key ||
($value_is_array && in_array($key, $element['#value'])))) {
$selected = ' selected="selected"';
}
else {
$selected = '';
}
// #vishal this is where the variable is being used.
$options .= '<option title="'.$vtitle.'" value="' . check_plain($key) . '"' . $selected .
'>' . check_plain($choice) . '</option>';
}
}
return $options;
}
below is the correct code for the last theme function
function kt_vusers_form_select_options($element, $choices = NULL, $vtitles=NULL) {
// Build up your own version of form_select_options here
// that takes into account your extra attribute needs.
// This will probably involve inspecting your custom FAPI property,
// which we'll call #extra_option_attributes
if (!isset($choices)) {
$choices = $element['#options'];
$vtitles = array();
$vtitles = $element['#title'];
}
// array_key_exists() accommodates the rare event where $element['#value'] is NULL.
// isset() fails in this situation.
$value_valid = isset($element['#value']) || array_key_exists('#value', $element);
$value_is_array = $value_valid && is_array($element['#value']);
$options = '';
// print_r($vtitles);
while
(
(list($key, $choice) = each($choices))
&& (list($keytwo, $vtitle) = each($vtitles))
)
{
// printf("%s => %s, %s => %s \n", $key1, $value1, $key2, $value2);
if (is_array($choice)) {
$options .= '<optgroup label="' . $key . '">';
$options .= kt_vusers_form_select_options($element, $choice);
$i++;
// $options .= form_select_options($element, $vtitle);
$options .= '</optgroup>';
} // end if if is_array
elseif(is_object($choice)) {
$options .= form_select_options($element, $choice->option);
} // end of else if
else {
$key = (string) $key;
if ($value_valid && (!$value_is_array && (string) $element['#value'] === $key ||
($value_is_array && in_array($key, $element['#value'])))) {
$selected = ' selected="selected"';
}
else {
$selected = '';
}
// $options .= '<option title="'.$vtitle.'" value="' . check_plain($key) . '"' .
$selected . '>' . check_plain($choice) . '</option>';
}
$options .= '<option value="'. check_plain($key) .'" title="' . $vtitle . '"' . $selected
.'>'. check_plain($choice) .'</option>';
} // end of choice
return $options;
} // end of function

I'm afraid you're going to have to dig quite far down to do this, the #options array is flattened in form_select_options() and it doesn't currently include any way of adding attributes. This is the code:
$options .= '<option value="' . check_plain($key) . '"' . $selected . '>' . check_plain($choice) . '</option>';
As you can see there's simply no scope for attributes in there. You will be able to override this though, but it will involve implementing your own version of theme_select() and your own FAPI property.
I haven't got time to flesh the entire thing out but in your theme file you would be doing something like this:
function MYTHEME_select($variables) {
$element = $variables['element'];
element_set_attributes($element, array('id', 'name', 'size'));
_form_set_class($element, array('form-select'));
return '<select' . drupal_attributes($element['#attributes']) . '>' . MYTHEME_form_select_options($element) . '</select>';
}
function MYTHEME_form_select_options($element) {
// Build up your own version of form_select_options here
// that takes into account your extra attribute needs.
// This will probably involve inspecting your custom FAPI property,
// which we'll call #extra_option_attributes
}
Refer to form_select_options for constructing MYTHEME_form_select_options
And your code in the form would be something like:
$form['select'] = array(
'#type' => 'select',
'#options' => array(1 => 'One', 2 => 'Two'),
'#extra_option_attributes' => array(
1 => array('title' => 'Test title'), // Attributes for option with key "1",
2 => array('title' => 'Test title'), // Attributes for option with key "2",
)
);
In MYTHEME_form_select_options() you can then inspect the element's #extra_option_attributes key (if one exists) to see if you need to physically add any more attributes in the HTML you create.
Hope that helps, I know it seems like a crazily long-winded way of doing what you need to but as far as I know it's the only way.

Tested
Try:
$form["tid"][1]['#attributes'] = array('title' => t('nooo chatter'));
instead of:
$form["tid"]['#options'][1]['#attributes'] = array('title' => t('nooo chatter'));
It is possible to add arbitrary attributes to elements this way. I discovered this after performing some tests based on this answer.
Also mentioned here and here.

Untested, but have you tried:
$form["tid"]["#options"][1]['#attributes'] = array('title' => t('YOUR NEW TITLE'));
You might need to mess around with the name of the element etc but the #attributes tag should work.
EDIT: As pointed out in Clive's answer, this won't work but I'll leave it up in case anyone wants to know how to add attributes to other fields (textboxes etc).

Related

WordPress: cannot access callback_args key values inside callback function

I am developing a simple plugin based custom post type. The post type is registered fine. Now I want to create some meta boxes and pass a few property values via callback arguments. This is what I tried:
function wpcd_add_dealer_meta_boxes() {
add_meta_box(
'dealer_first_name',
'First Name',
array($this, 'wpcd_meta_box_first_name_markup'),
'dealers',
'normal',
'default',
array(
'id' => 'first_name',
'name' => 'first_name',
'type' => 'text',
'placeholder' => 'Enter first name',
'maxlength' => '30',
'spellcheck' => 'true',
'autocomplete' => 'off'
)
);
}
And here is my callback function with an argument:
function wpcd_meta_box_first_name_markup($args) {
$html = '<input ';
$html.= 'type="' . $args->type . '" ';
$html.= 'id="' .$args->id . '" ';
$html.= 'name="' .$args->name . '" ';
if( isset($args->required) && ($args->required == 'true' || $args->required == '1' ) ) {
$html.= 'required ';
}
if( isset($args->placeholder) && $args->placeholder != '' ) {
$html.= 'placeholder="' . esc_attr( $args->placeholder ) . '" ';
}
if( isset($args->maxlength) ) {
$html.= 'maxlength="' . $args->maxlength . '" ';
}
if( isset($args->spellcheck) && ($args->spellcheck == 'true' ) ) {
$html.= 'spellcheck="' . $args->spellcheck . '" ';
}
if( isset($args->autocomplete) && ($args->autocomplete == 'on' ) ) {
$html.= 'autocomplete="' . $args->autocomplete . '" ';
}
$html.= '/>';
echo $html;
}
But I cannot get the values like $args->id, $args->name etc inside the function. To be precise, all the values are empty, where I did not check if(isset(...)). And where I did those are simply ignored.
With my above code I am expecting the following markup as output:
<input type="text" id="first_name" name="last_name" required placeholder="Enter firstname" maxlength="30" autocomplete="off" spellcheck="true" />
while the actual output is
<input type="" id="" name="" />
Attributes type, id and name are not wrapped inside an if(isset()) block so they are being generated (with empty values) and whatever is wrapped inside if(isset()) block, are simply being ignored, as if they are not set at all!
What I am missing or doing wrong?
Any suggestion would be a life saver for me.
If you check the documentation for add_meta_box() carefully, you'll see:
($callback_args (array) (Optional) Data that should be set as the
$args property of the box array (which is the second parameter passed
to your callback).
The first parameter passed to your callback is a WP_Post object. The second is your parameter array. So, try:
function wpcd_meta_box_first_name_markup($post, $args) { ...
And then access your arguments as you'd expect:
$args['type']

Wordpress: How to Search within a specific category by slug (category name)

How can I create a code which would search by slug(category name) but not by ID. How can I create a customized solution for my client for search within a specific category (without any plugins)?
Explain: I want the code of search to be customized in such a way that the client could search within a specific category without editing any code. Thanks!
is it's still actually, you should to check this tutorial where an author gives a very detailed explanation about the Search function customization.
One of the most interesting parts for a necessary your case:
// meta_query expects nested arrays even if you only have one query
$sm_query = new WP_Query( array( 'post_type' => 'accommodation', 'posts_per_page' => '-1', 'meta_query' => array( array( 'key' => '_sm_accommodation_city' ) ) ) );
// The Loop
if ( $sm_query->have_posts() ) {
$cities = array();
while ( $sm_query->have_posts() ) {
$sm_query->the_post();
$city = get_post_meta( get_the_ID(), '_sm_accommodation_city', true );
// populate an array of all occurrences (non duplicated)
if( !in_array( $city, $cities ) ){
$cities[] = $city;
}
}
}
} else{
echo 'No accommodations yet!';
return;
}
/* Restore original Post Data */
wp_reset_postdata();
if( count($cities) == 0){
return;
}
asort($cities);
$select_city = '<select name="city" style="width: 100%">';
$select_city .= '<option value="" selected="selected">' . __( 'Select city', 'smashing_plugin' ) . '</option>';
foreach ($cities as $city ) {
$select_city .= '<option value="' . $city . '">' . $city . '</option>';
}
$select_city .= '</select>' . "\n";
reset($cities);

show/hide a part of shortcode if an attribute value is 0

hi guys i want to hide a part of shortcode when an attribute value like closebutton is 0.
this is the code that i use
function atalertshortcode($atts, $content = null) {
extract(shortcode_atts(array(
"class" => '',"text" => '',"size" => '',"closebutton" => '',"other" => ''
), $atts));
return '<div class="uk-alert '.$class.' '.$size.'" data-uk-alert="" '.$other.'> '.$text.' </div>';}
i want to hide this part of shortcode when closebutton value is 0
please help me
Here you go:
function atalertshortcode($atts, $content = null) {
extract(shortcode_atts(array(
"class" => '',
"text" => '',
"size" => '',
"closebutton" => '',
"other" => '',
), $atts));
$output = '<div class="uk-alert ' . $class . ' ' . $size . '" data-uk-alert="" ' . $other . '>';
if ($closebutton) {
$output .= '';
}
$output .= ' ' . $text . '</div>';
return $output;
}
This basically requires you to separate the output in several pieces, and wrap the optional portion in an IF block to check the required condition (in this case we're checking if $closebutton does not evaluate to 0 in any way, and if it doesn't, the link is added to the shortcode output).

Drupal: Edit email template from contact form

When submitting a message in my site-wide contact form in Drupal 6.x I get the following message along the top of every message:
[Name] sent a message using the contact form at [www.mysite.com/contact]
I would like to remove this message. Looking around, I've found it comes from the contact.module here:
$message['body'][] = t("!name sent a message using the contact form at !form.", array('!name' => $params['name'], '!form' => url($_GET['q'], array('absolute' => TRUE, 'language' => $language))), $language->language);
I've done a bit of research and it seems that I need to create a custom module with a hook_mail_alter() function to edit the contact.module. When it comes to this I get a bit lost. Could anyone kindly take me through the steps to accomplish the task?
Many thanks.
I did something like that recently. Here is a template you can use to get what you need. Most is from the contact module. The code below is from Drupal 7 but should work as is in Drupal 6.
/**
* Implementation of hook_mail_alter().
*/
function modulename_mail_alter(&$message) {
if ($message['id'] == 'contact_page_mail') {
$language = $message['language'];
$params = $message['params'];
$variables = array(
'!form-url' => url($_GET['q'], array('absolute' => TRUE, 'language' => $language)),
'!sender-name' => format_username($params['sender']),
'!sender-url' => $params['sender']->uid ? url('user/' . $params['sender']->uid, array('absolute' => TRUE, 'language' => $language)) : $params['sender']->mail,
);
$message['body'] = array();
$message['body'][] = t("Your custom message with variables", $variables, array('langcode' => $language->language));
$message['body'][] = $params['message']; // Append the user's message/
}
}
function theme_mail_alter(&$message) {
// only alter contact forms
if (!empty($message['id']) && $message['id'] == 'contact_page_mail') {
$contact_message = $message['params']['contact_message'];
$message['body'] = [];
$fields = $contact_message->getFields();
$new_body .= 'Message:' . PHP_EOL . $contact_message->get('message')->value . PHP_EOL . PHP_EOL;
foreach ($fields as $field_name => $field) {
if (get_class($field->getFieldDefinition()) == 'Drupal\field\Entity\FieldConfig') {
$new_body .= $field->getFieldDefinition()->label() . ':' . PHP_EOL;
if (isset($contact_message->get($field_name)->entity->uri->value)) {
$uri = $contact_message->get($field_name)->entity->uri->value;
$url = file_create_url($uri);
$new_body .= $url . PHP_EOL . PHP_EOL;
} else {
$new_body .= $contact_message->get($field_name)->value . PHP_EOL . PHP_EOL;
}
}
}
$message['body'][] = $new_body;
}
}

Wordpress Prevent Users from posting full size image uploads

Is there any way in Wordpress to prevent content editors from selecting the "Full size" option when uploading images to a post? I'd like them to just have the "thumbnail", "medium", and "large" options. I used to use the Scissors plugin to do this, but as of Wordpress 2.9 this plugin no longer works.
You could acheive this result by forcing WordPress not to display the full size option. The function that creates the size radio buttons is in wp-admin/includes/media.php and is called image_size_input_fields.
There's no filter or action hook for that function that I'm aware of, but the function that calls it (image_attachment_fields_to_edit) has a filter hook of attachment_fields_to_edit.
So basically we can use the filter hook to override those two functions with our own, which will only be very slightly modified.
This will work in the standard functions.php file, or I suppose you could incorporate it into a plugin.
First, add the new filter:
add_filter('attachment_fields_to_edit', 'MY_image_attachment_fields_to_edit', 11, 2);
Next we create our two functions. I've just prefixed the names with MY_ for this case:
function MY_image_attachment_fields_to_edit($form_fields, $post) {
if ( substr($post->post_mime_type, 0, 5) == 'image' ) {
$alt = get_post_meta($post->ID, '_wp_attachment_image_alt', true);
if ( empty($alt) )
$alt = '';
$form_fields['post_title']['required'] = true;
$form_fields['image_alt'] = array(
'value' => $alt,
'label' => __('Alternate text'),
'helps' => __('Alt text for the image, e.g. “The Mona Lisa”')
);
$form_fields['align'] = array(
'label' => __('Alignment'),
'input' => 'html',
'html' => image_align_input_fields($post, get_option('image_default_align')),
);
$form_fields['image-size'] = MY_image_size_input_fields( $post, get_option('image_default_size', 'medium') );
} else {
unset( $form_fields['image_alt'] );
}
return $form_fields;
}
The only thing that's changed here from the normal WordPress function is that we're calling MY_image_size_input_fields instead of image_size_input_fields.
Now the function that does the actual hiding:
function MY_image_size_input_fields( $post, $check = '' ) {
// get a list of the actual pixel dimensions of each possible intermediate version of this image
/* $size_names = array('thumbnail' => __('Thumbnail'), 'medium' => __('Medium'), 'large' => __('Large'), 'full' => __('Full size')); */
$size_names = array('thumbnail' => __('Thumbnail'), 'medium' => __('Medium'), 'large' => __('Large'));
if ( empty($check) )
$check = get_user_setting('imgsize', 'medium');
echo '<pre>'; print_r($check); echo '</pre>';
foreach ( $size_names as $size => $label ) {
$downsize = image_downsize($post->ID, $size);
$checked = '';
// is this size selectable?
$enabled = ( $downsize[3] || 'large' == $size );
$css_id = "image-size-{$size}-{$post->ID}";
// if this size is the default but that's not available, don't select it
if ( $size == $check ) {
if ( $enabled )
$checked = " checked='checked'";
else
$check = '';
} elseif ( !$check && $enabled && 'thumbnail' != $size ) {
// if $check is not enabled, default to the first available size that's bigger than a thumbnail
$check = $size;
$checked = " checked='checked'";
}
$html = "<div class='image-size-item'><input type='radio' " . ( $enabled ? '' : "disabled='disabled' " ) . "name='attachments[$post->ID][image-size]' id='{$css_id}' value='{$size}'$checked />";
$html .= "<label for='{$css_id}'>$label</label>";
// only show the dimensions if that choice is available
if ( $enabled )
$html .= " <label for='{$css_id}' class='help'>" . sprintf( __("(%d × %d)"), $downsize[1], $downsize[2] ). "</label>";
$html .= '</div>';
$out[] = $html;
}
return array(
'label' => __('Size'),
'input' => 'html',
'html' => join("\n", $out),
);
}
In this last function only two things change. At the top we get rid of the reference to 'Full Size' in the $size_names array definition. Then the line that says $enabled = ( $downsize[3] || 'large' == $size );, we changed. We simply replaced 'full' == $size with 'large' == $size.
Here's a screenshot of the result:
I don't think you can disable the large size, but you can set it's width and height to be the same as the medium size. It's under Settings → Media

Resources