Drupal change multi select to checkboxes on views exposed form not working - drupal

I needed to have all taxonomy vocabularies available to filter on, without adding all of the taxonomies one at a time.
I did this using the Content: Has taxonomy terms (Multiple) Filter - which renders out as a multiple select list.
I needed to change the select list to checkboxes, but BEF didnt allow me to do that for this type of field, so i did the following...
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'views_exposed_form') {
$options = $form['filter']['#options'];
unset($form['filter']);
foreach($options as $vocab => $terms) {
foreach ($terms as $key => $value) {
$newkey = $options[$vocab][$key]->option;
$termoptions[$vocab][key($newkey)] = $newkey[key($newkey)];
}
$form[$vocab] = array(
'#type' => 'checkboxes',
'#options' => $termoptions[$vocab],
'#title' => $vocab,
'#multiple' => TRUE,
);
}
}
}
The exposed form looks good, but it doesnt work.
I think its because the name of the query is wrong. As i have split up the heirarchy into separate fields, the url used to look like
mysite.com/category?filter[]=123
Now it looks like...
mysite.com/category?Brand[123]=123
So theres how far ive got, any ideas how i can make this exposed form work?
I had a poke around at changing the submit handler views_exposed_form_submit but i dont know what i would need to change.

i am updating my ans once again for more clear view
step one expose filter
settings for this
click on settings text then
then gor for bef
and the result is
hope it make sense now

Related

How to prevent Drupal 7 from converting & to &

I'm adding placeholder to search form like this:
$form['search_block_form']['#attributes']['placeholder'] = ' Search';
But problem is that drupal is printing & sign as & so in HTML output
for placeholder attribute instead of:
placeholder=" Search"
I'm getting:
placeholder=" Search"
How to prevent this behavior or is there another way to print that character by it's hex value? Tried using:
[] and decode_entities('') and some other functions, but none of them helped.
I think you were close with decode_entities(), but you could try the PHP function html_entity_decode().
However, unless the field you are trying to add the place holder to is called 'search_block_form', I think it is your construct which is wrong.
If you are doing this is a hook_form_alter() (which you should be if you are writing it like this) and the field you want to add the placeholder to is called "my_text_field", then the function should look for like this:
function custom_module_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'custom_form') {
$form['my_text_field']['#attributes']['placeholder'] = html_entity_decode(' Search');
}
}
Just remember to replace 'custom_form' with the real ID of your form and 'my_text_field' with the name of your field you want to alter.
/**** EDIT ****/
Finally understood what you are trying to do. To get this working on my site I had to lay my form item out like this:
$form['test'] = array(
'#type' => 'textfield',
'#attributes' => array(
'placeholder' => html_entity_decode(''),
'style' => array('font-family: Arial, FontAwesome;'),
),
);
The important thing to add is the style attribute of font family and make sure it includes FontAwesome.
So the above example for you would become:
function custom_module_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'custom_form') {
$form['my_text_field']['#attributes']['placeholder'] = html_entity_decode(' Search');
$form['my_text_field']['#attributes']['style'] = 'font-family: Arial, FontAwesome;';
}
}
Not sure how you have added the JS for Font Awesome, but I added it in a page alter using drupal_add_js() so that I could make sure it only get loaded on the pages its required.
Let me know how that works out for you, and don't forget the flush your caches too!

datagrid filter for relation object as text field (insted of dropdown) in sonata admin in symfony 2.4

I have entity 'Action' with relation to 'User'. Created Admin CRUD controller in SonataAdminBundle. Everything works fine except user filter is rendered as dropdown list. I have 8k user count and growing so you must see why this is a problem.
I want user filter to be text input and on submit to search with LIKE %username%
Right now I add user filter like this - $datagridMapper->add('user').
I know I can add filter type and field type but I am not able to find the right combination and options. Found information on http://sonata-project.org/bundles/doctrine-orm-admin/master/doc/reference/filter_field_definition.html but still no success.
Final solution
Following Alex Togo answer I used this code:
$datagridMapper->add('user', 'doctrine_orm_callback', array(
'callback' => function($queryBuilder, $alias, $field, $value) {
if (empty($value['value'])) {
return;
}
$queryBuilder->leftJoin(sprintf('%s.user', $alias), 'u');
$queryBuilder->where('u.username LIKE :username');
$queryBuilder->setParameter('username', '%'.$value['value'].'%');
return true;
},
'field_type' => 'text'
))
I needed something like this on a project. I implemented this feature using this. You can try to set the 'field_type' option to 'text' (I used 'choice' in the project I worked at) and add the querybuilder actions you need to filter.
Use doctrine_orm_choice option.
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper->add(
'module',
'doctrine_orm_choice',
[],
'choice',
[
'choices' => $this->filterModuleList
]
)
....

Silverstripe tumblr-like Post Types

I am trying to create a back-end interface for silverstripe that gives the CMS user the option to choose between a set of Post Types (like tumblr) in Silverstripe3. So they can choose to create a News Post, Video Post, Gallery Post, etc.
I initially started off giving all Posts the necessary fields for each Type and adding an enum field that allowed the user to choose the Post Type. I then used the forTemplate method to set the template dependent upon which Post Type was chosen.
class Post extends DataObject {
static $db = array(
'Title' => 'Varchar(255),
'Entry' => 'HTMLText',
'Type' => 'enum('Video, Photo, Gallery, Music')
);
static $many_many = array(
'Videos' => 'SiteVideo',
'Photos' => 'SitePhoto,
'Songs' => 'SiteMp3'
);
public function forTemplate() {
switch ($this->Type) {
case 'Video':
return $this->renderWith('VideoPost');
break;
case 'Photo':
return $this->renderWith('ImagePost');
break;
etc...
}
function getCMSFields($params=null) {
$fields = parent::getCMSFields($params);
...
$videosField = new GridField(
'Videos',
'Videos',
$this->Videos()->sort('SortOrder'),
$gridFieldConfig
);
$fields->addFieldToTab('Root.Videos', $photosField);
$photosField = new GridField(
'Photos',
'Photos',
$this->Photos()->sort('SortOrder'),
$gridFieldConfig
);
$fields->addFieldToTab('Root.Videos', $photosField);
return $fields;
}
}
I would rather the user be able to choose the Post Type in the backend and only the appropriate tabs show up. So if you choose Video, only the Video GridField tab would show up. If you choose Photo Type only the Photo's GridField would show.Then I would like to be able to call something like
public function PostList() {
Posts::get()
}
and be able to output all PostTypes sorted by date.
Does anyone know how this might be accomplished? Thanks.
Well the first part can be accomplished using javascript. Check out this tutorial and the docs let me know if you have questions on it.
The second part would be trickier but I think you could do something with the page controller. Include a method that outputs a different template based on the enum value but you would have to set links somewhere.
I managed this with DataObjectManager in 2.4.7 as I had numerous DataObjects and all were included in one page but I'm not sure if that is feasible in SS3.
return $this->renderWith(array('CustomTemplate'));
This line of code will output the page using a different template. You need to include it in a method and then call that method when the appropriate link is clicked.

how to add a button to drupal forms?

I want to add a custom button to drupal create form so if the user clicked on it instead of submit button, the workflow state of the created object change to another state(not the default first state)
any suggestion?
To modify the default forms generated by drupal you have to add a form_alter hook to your module. You can do it by defining a function like modulename_form_alter assuming the name of your module is modulename. The drupal system passed the form array and the form_state array which you can use to override the default behavior. In your case, the complete function would look something like this.
function modulename_form_alter(&$form, $form_state, $form_id) {
if($form_id == 'what you want') {
$form['buttons']['another_button'] = array(
'#type' => 'submit',
'#value' => 'Add to another state',
'#submit' => array('modulename_custom_form_submit')
);
}
}
function modulename_custom_form_submit($form, &$form_state) {
if($form_state['values']['another_button'] == 'Add to another state') {
//Do your thing
}
}
After you made the necessary modifications, you can simply submit to the default submit action of the creation form.

Drupal, template.php where do the $form names come from?

I want to customize my Drupal back-end forms.
I'm using template.php file.. i.e.
$form['menu']['#collapsed'] = true;
$form['author']['#collapsed'] = true;
$form['buttons']['#weight'] = 100;
But I was wondering from where the section names (menu, author, buttons), come from. (They are not id or classes in html code, so I guess there is an index with all names stored somewhere.
Where can I get the complete list of section names ?
For example, what are the names for revision and publishing sections ? 'revision', 'publish', 'publishing' don't work.
thanks
If I am not mistaken, you want to see structure of some forms. Each form in drupal has an Id. First, you need to know the form_id. You can do this with a custom module and implementation of hook_form_alter:
function mymodule_form_alter(&$form, $form_state, $form_id) {
drupal_set_message($form_id);
}
When you have found the Id, alter the snippet to prints out the form structure:
function mymodule_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'a_form_id') {
drupal_set_message(print_r('<pre>'. $form .'</pre>', true));
// If you have installed Devel module, following line is much more readable:
// dpm($form);
}
}
Now when you go to the page containing the form, you see it's structure.Each form element is represented as an array, for example, a text field can be like this:
$form['name'] = array(
'#type' => 'textarea',
'#title' => t('Username')
);
Look for Form API in Drupal website for more info.
I don't think there actually is a naming system with forms like that. The names is most likely the same used when defining the form, which could be anything really. Drupal core might be consistent, but if you want to add contrib modules, you can't be sure of anything.

Resources