drupal theme checkboxes - drupal

I'm trying to us the theme hook the style some checkboxes. I have used the form_alter hook to make some changes to the labels of each checkbox and now i would like to interact with the checkbox theme function to style the labels.
In the form array it says
#theme (String, 20 characters ) select_as_checkboxes
is this the function i need to override? And how do i do this?
I tried stuff like
function mymodule_select_as_checkboxes()
but i can't get it to work

Solution:
function my_module_theme_registry_alter(&$theme_registry) {
// Register theme_function to use the module custom function
if (!empty($theme_registry['select_as_checkboxes'])) {
$theme_registry['select_as_checkboxes']['function'] = 'my_module_select_as_checkboxes';
}
}
Then in the function my_module_select_as_checkboxes create your own function or adapt the original one.

Related

How I can print all variable of a hook in drupal 8?

I'm very new in Drupal 8 and I have issue now with hook. Mainly I though that I don't clearly understand structure and hook definition in Drupal 8.
So my main problem is that I have some hook to interact with main menu (add custom class name to ul, li and link, a tag). I can do it by changing template file and now try to do it with any hook.
Although I found that some hook relating to menu ex. hook_contextual_links_alter (link: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Menu%21menu.api.php/function/hook_contextual_links_alter/8.9.x).
At the end of this hook we have the code related:
function hook_contextual_links_alter(array &$links, $group, array $route_parameters) {
if ($group == 'menu') {
// Dynamically use the menu name for the title of the menu_edit contextual
// link.
$menu = \Drupal::entityTypeManager()
->getStorage('menu')
->load($route_parameters['menu']);
$links['menu_edit']['title'] = t('Edit menu: #label', [
'#label' => $menu
->label(),
]);
}
}
So I have installed devel module with kint function and in my .theme file and try:
function hook_contextual_links_alter(array &$links, $group, array $route_parameters) {
kint($links);
}
and then reload my Home page but nothing showed. But I can get some information about other like:
function eg_learn_theme_suggestions_page_alter(&$suggestions, $variables) {
kint($suggestions);
}
So what happens here? Can you help to explain if how I can print the variable of this hook (in .theme file) and the site page to see the printing variable?
In general when I found a hook, how I can print there array and check it in website?
There are some problems about your approach:
When implementing a hook, you must replace "hook" with the module name/theme name where you put the hook function inside. For example, if you want implement hook_contextual_links_alter in your_custom module, it becomes your_custom_contextual_links_alter().
Not all hook can be implemented in the theme. Some hook can only be implemented in modules (in .module file). You can read more here.
In your case, I think hook_preprocess_menu would be more suitable. You can implement it in your custom theme like this:
function <your_theme_name>_preprocess_menu(&$variables) {
if ($variables['menu_name'] == 'main') {
kint($variables);
}
}

How to filter pluggable function from wordpress child-theme

I need to add a filter to a specific wordpress function which is defined in a file which is included by a pluggable function theme's functions f
functions.php:
if (!function_exists('nectar_colors_css_output')) {
function nectar_colors_css_output(){
include('css/colors.php');
}
}
colors.php:
<?php
function nectar_colors() {
// not relevant what happens here, but I have
// to call another function before this one is called!
}
?>
I use a child theme and when I try to filter this function from the child theme's functions.php, nothing happens. This is because pluggable functions of the parent theme will be called after calling the filters from the child theme.
My filter function in the child theme's functions.php is
function filter_function() {
// some custom actions...
}
add_filter('nectar_colors', 'filter_function');
How can I get this filter working?
What exactly are you trying to filter on? You might be misunderstanding the concepts.
Take your example provided. The parent checks if a function exists before creating it.
So basically, if you define it in your child's theme, the parent will know it's there and won't create a default one.
#themename-child
function nectar_colors_css_output(){
error_log("It worked!");
}

Drupal 7 - Taxonomy terms hierarchy hook function

As you know there is an admin page for setting up hierarchy of terms by dragging it, which can be found on admin/structure/taxonomy/your_vocabulary. Underneath the table there are two buttons "Save" and "Reset to alphabetical". Now I need to interact with those sumbits by using some hook but I've no idea how to do it. I've already tried hook_taxonomy_term_presave() and hook_taxonomy_term_update(), but those are definitely not appropriate. Any ideas how to hook it?
You can do all your stuff by adding additional callback in submit.
e.g:
/**
* Implements hook_form_FORM_ID_alter().
*/
function hook_form_taxonomy_form_vocabulary_alter(&$form) {
$form['#submit'][] = 'my_function';
}
function my_function(&$form, &$form_state) {
// Do something ..
}

how do I call my theme preprocess function for a specific field?

i'm on Drupal 7 and i have aspecific tpl.php file for a content field_image: "field--field_image.tpl.php". I need to create a preprocess function for this field and for my theme.
Supposing my theme name is "My Theme"
It should look like
function my_theme_preprocess_field(&$variables, $hook) {
$variables['classes_array'][] = 'aClassName';
}
but it doesn't work. I am wrong. But where?
Thanks
You can use template_preprocess_field() (like you do in your code above) but just test the particular field is the right one for you:
function my_theme_preprocess_field(&$variables, $hook) {
$element = $variables['element'];
if (isset($element['#field_name'])) {
if ($element['#field_name'] == 'field_image') {
$variables['classes_array'][] = 'aClassName';
}
}
}
Once you've implemented the hook don't forget to clear your caches, hook implementations are cached in Drupal 7 so won't be picked up until the cache is cleared.
You could declare a mytheme_preprocess_field(&$variables, $hook) in your theme's template.php where you can check your field and do operations on its label or markup, add classes, anything. So you wouldn't need field specific tpls. - eg.
function mytheme_preprocess_field(&$variables, $hook) {
if ($variables['element']['#field_name'] == 'field_machine_name') {
$variables['items'][0]['#markup'] = 'add custom markup';
}
}
Hope this helps someone.
In drupal 7 you can rewrite output of the field in template_preprocess_node() by altering "#markup" value of the field.
Also you can use regexp to change whatever you want in page content :)

Overriding an input of the Exposed Views form to render a range of values

In the Exposed Views form (Drupal 6, Views 2), I'd like to overwrite an input text so it render the option to choose from a range of values, something similar to this:
Or just a simple one: two text inputs with from and to labels (min, max, whatever, like a price range).
I am comfortable both with editing templates (views-exposed-form.tpl.php in this case) and overwritting the form output:
function mymodule_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'views_exposed_form') {
$form['submit']['#value'] = t('Filter');
}
}
How can I change the defaul text input to one as described?
I am doing something similar as a template function.
You can remove the rendered output and render your own as a theming function.
function THEME_preprocess_views_exposed_form(&$vars, $hook) {
switch($vars['form']['#id']){
case 'views-exposed-form-VIEW_NAME-page-1':
$vars['widgets']['filter-YOUR_FIELD']->widget = YOUR_CODE_HERE;
}
}
Be sure to use dpm() from the Devel module to figure the identifiers you need to use because might be slightly different in the theme layer.

Resources