batigolix
  • Member for 9 years, 1 month
  • Last seen more than a month ago
How to assign permissions to roles programmatically?
21 votes

I'd like to add a code example that is closer to the original question on how to assign permissions to existing roles programmatically. You can look up the roles with their name, retrieve the role ID ...

View answer
How do I customize the search result page?
18 votes

In Drupal 7 you use ... template_preprocess_search_results and template_preprocess_search_result ... to tinker with the information displayed in the results. And you use ... search-results.tpl.php ...

View answer
How to logout all active users?
15 votes

You can empty the sessions table using drush drush sqlq "TRUNCATE sessions" or if you have set prefixes for table names: drush sqlq --db-prefix "TRUNCATE {sessions}"

View answer
Programmatically create a user and assign a role
15 votes

To programmatically create a user with roles and custom field values (like e.g. First Name and Last Name) you can use the following code: $new_user = array( 'name' => 'xgramp', 'pass' => '...

View answer
Why is hook_update_N not running?
14 votes

Another reason why an update hook would not run is a mismatch between the update hook name and the major release number of the module (see .info file). For example: the update hook ...

View answer
How to preserve kint output upon form submit redirection?
9 votes

Use ksm() instead of kint(). I ran into the same issue when I want to inspect the entity after saving, using drupal's hook_entity_presave and kint I found this article on how to print variables ...

View answer
How can I hide or show fields using states with hook form alter
8 votes

It seems you re using the Drupal 7 syntax. For Drupal 8 this is slightly different. You can implement hook_form_alter() as follows to change the visibility state of a form element as follows: /** * ...

View answer
How do I add CSS & JavaScript to <head>?
7 votes

In Drupal 8, stylesheets and JavaScript files are attached as libraries: You add a yourtheme.libraries.info to your theme (or custom module) folder: power-slider: version: 1.x js: js/power-...

View answer
How do I clear or delete a queue?
6 votes

Yes, you can delete or clear a queue in the database by emptying the queue table. Drush allows you to do that easily with drush sqlq "TRUNCATE queue". Careful. I tried this in a local development ...

View answer
How to hide a custom user field in user profile?
6 votes

To hide the field from the user profile form you can set the #access property of a field to FALSE using hook_form_FORMID_alter. The following snippet hides the field field_organisation from the user ...

View answer
How to show fields in the Inline Entity Form display widget?
5 votes

To add to Mojeferous' answer, you do this indeed by implementing hook_inline_entity_form_table_fields_alter() in your module which is documented in the modules' inline_entity_form.api.php. You can ...

View answer
Custom ckeditor.styles.js file with WYSIWYG module
5 votes

These are 2 ways (there are certainly more) to add custom ckeditor stylesets using the drupal wyswiwyg module. Using the contributed module Ckeditor Styles Using hook_wysiwyg_editor_settings_alter ...

View answer
How to hide title of any content type without using a module?
4 votes

David is right, but if you want to use the $title elsewhere in the page you can use this approach. Step 1. Create a show/hide title variable using hook_preprocess_page() in your custom module (or ...

View answer
View mode template for user-profile.tpl.php?
3 votes

You already figured out how to create an additional view mode for the user entity: /** * Implements hook entity info alter * Adds additional view mode for user entity */ function ...

View answer
text_format field value not appearing in full_html mode
2 votes

You don't need to eliminate the #format. The format and value of the text_format are stored as an array. So in the same way as you retrieve the value you can retrieve the format. See this example: ...

View answer
Where are the primary and secondary tab blocks?
2 votes

They are available under other names (I think one is called "Tabs") when you used the the minimal installation profile. But you can import the blocks by entering the code of the files profiles/...

View answer
Theming and overriding taxonomy term/vocabulary page
2 votes

You can have full control over the taxonomy/term/ID page by intercepting the menu call with hook_menu_alter() and let the content be generated by a custom function. This allows you to completely ...

View answer
How to tell Drupal to look for templates in module directory?
2 votes

I like dashohoxha's solution of the hook_theme implementation but was not able to get it working. After some more googling I found a variation that worked nice for me: /** * Implements hook_theme(). ...

View answer
Translating interface - same word duplication
2 votes

You need to translate each of these strings individually. It is not enough to do it only once.

View answer
How do I install with PostgreSQL using Docksal?
1 votes

It should be noted that you need postgres 10+ when using Drupal 9. The steps that you are following in are for Drupal 8 and they will install postgres 9. Recently a services has been added to Docksal ...

View answer
Custom block with configurable message inside it
1 votes

This is a good example for a Drupal 8 custom block that is configurable by the user

View answer
Will migrating content types be possible?
1 votes

Bas is right, but there is a lot of activity going on regarding this topic, so here is a more up-to-date (November 2015) answer: Given the big difference in architecture between Drupal 7 and 8 there ...

View answer
Hide Title for Rendered Entity views
1 votes

Views renders the node entity using the default view mode (machine name 'full'). Drupal uses the node.tpl.php template to output the node. That template contains a condition to check for the $page ...

View answer
Display last logon date on profile
1 votes

You can implement template_preprocess_user_profile In your theme's template.php file add: function MYTHEMENAME_preprocess_user_profile(&$vars) { $vars['user_profile']['last_login'] = array( ...

View answer
How to get the translated term name by tid?
1 votes

Knibals code shows how to translate a full vocabulary tree. This code snippet shows how to translate one specific term. $term = taxonomy_term_load($tid); $translated_term = ...

View answer
Can we get full count of results from a query that is extended by PagerDefault
0 votes

You can get the total count of a query that is extended with PagerDefault with $num_rows = $query->countQuery()->execute()->fetchField(); This query fetches all nodes with pager and ...

View answer
Right way to create Yes/No boolean fields
0 votes

As follows: Field type: List (integer) & Widget: Check boxes/radio buttons Values: 1|Yes Number of values: unlimited See drupal.org

View answer
Limit which roles can view a node basing on its content type
0 votes

I used a code snippet found in the D7 api documentation for the hook_node_access hook. This code will grant access to view "ebook" content to users that have the "view ebook" permission. You need a ...

View answer
How to use prepareKey() for the Migrate module
0 votes

You can set the $item_ID_xpath to something that does exist in the XML, for example 'name' Then inside your migration class, you can use the prepareKey method to generate an ID public function ...

View answer
How to remove the RSS feed icon form the content listing pages?
0 votes

As marblegravy correctly suggest you should indeed remove the link from your theme. This is different for each theme, but for example in Bartik you can remove the following line in page.tpl.php: <?...

View answer