Hot answers tagged

40

If you are trying to send the mails via your custom module then you can follow the below mentioned guide. Say, I had one Module called "Commerce Canvas(commerce_canvas)" 1. First Alter the Drupal's mail function to support links and add other encoding types /** * Implements hook_mail_alter() * @param string $message */ function ...


27

As mentioned by Ivan Jaros You can use hook_preprocess_page_title. You just have to load the node from the route first, to get some context. function yourtheme_preprocess_page_title(&$variables) { // Load the node entity from current route if ($node = \Drupal::routeMatch()->getParameter('node')) { // Load the label of the bundle $...


20

When Drupal runs cron tasks, it automatically handles any cron queue defined from modules, in drupal_cron_run(); first hook_cron() implementations are invoked, and then the cron queues are emptied. Inmplementing hook_cronapi(), you can add an entry for another function handling the cron queue of your module. function mymodule_cronapi($op, $job = NULL) { $...


17

hook_node_view() hasn't really been removed, the way it's invoked has just changed. As you mentioned, it's now handled by hook_ENTITY_TYPE_view(). So you can either use: hook_entity_view() or hook_node_view() The latter would make more sense if you're targeting nodes specifically.


17

You need to implement the hook_entity_presave() /** * Implements hook_entity_presave(). */ function YOUR_MODULE_entity_presave(Drupal\Core\Entity\EntityInterface $entity) { switch ($entity->bundle()) { // Here you modify only your day content type case 'day': // Setting the title with the value of field_date. $entity->setTitle($...


16

In Drupal forms, form_alter hooks can be used to change almost anything in the form. Additional submit handlers can be handled, validation can be done, elements can be added, etc. But for all of these to work, Drupal need to be the responsible party in both form build phase and the form submit phase. When you set $form['#action'] = url('https://[url path ...


16

To get the content type of a node, you just use $variables['node']->getType() or $variables['node']->bundle(). The first method just calls the second, so calling one or the other one doesn't make much difference. public function getType() { return $this->bundle(); } The only difference is that bundle() is defined for every entity, since the ...


16

You can't listen to an event. You can either use a hook or implement a method like postSave() in your custom entity. Here is the order of hooks and other events that happen during an entity save: Entity::preSave() is called on entity objects, and FieldItemListInterface::preSave() on field objects. hook_ENTITY_TYPE_presave() hook_entity_presave() Entity is ...


15

To call the function of one module in another module: If the function is not in the .module file, call module_load_include() //Load node.admin.inc from the node module module_load_include('inc', 'node', 'node.admin'); Call the function from the other module function_you_want_to_call();


15

You can get the display ID through the current_display property of the View object, e.g. $view = $vars['view']; if ($view->name == 'foo' && $view->current_display == 'bar') { // ... }


14

You can do this by creating a theme suggestion alter hook in your theme. /** * Implements hook_theme_suggestions_user_alter(). * * An array of alternate, more specific names for template files or theme * functions. */ function MYTHEME_theme_suggestions_user_alter(&$suggestions, $vars, $hook) { // Define the view mode. $mode = $vars['...


13

When developing a module, you should keep in mind the following notes. The implementation of a hook done from a module is a function whose name is prefixed with the module short name (also called the machine name); from the hook name, take off the hook part, and replace it with the module machine name. For example, the implementation of hook_menu() done ...


13

Probably a little late to respond, but I came across this thread this morning whilst looking for a way to override the breadcrumb in Drupal 7, it helped a little so I thought I would add my solution. This creates extra crumbs based on content types and adds the current page to the crumb. I'm using this to add a crumb to a filtering view seen before the ...


13

You can implement the new hook_theme_suggestions_HOOK_alter() hook. Alters named suggestions for a specific theme hook. This hook allows any module or theme to provide altenative theme function or template name suggestions and reorder or remove suggestions provided by hook_theme_suggestions_HOOK() or by earlier invocations of this hook. You can use ...


13

Here is a working example I have pulled from a project and altered to have your module name. Add this at the top of your .module file: use Drupal\Core\Form\FormStateInterface; Then copy this in /** * Implements hook_form_alter(). */ function constellator_form_alter(&$form, FormStateInterface $form_state, $form_id) { // Adding custom validation ...


12

It sounds like you've already got a custom module, but just in case you haven't see the Module Developer's Guide. The hooks you're looking for are hook_node_insert() and hook_node_update(), and the function to clear the cache will either be cache_clear_all() or drupal_flush_all_caches. The former will let you clear specific cache bins (and even specific ...


12

The correct way of doing this is to respect Drupal's db transaction. If you want to do any database action in hook_node_insert you must consider that the node is not actually saved yet. which means if it fails and rolled back you end up with orphaned data in your database. So Drupal database API should be used and not direct query execution. If you want to ...


12

You should provide a fallback type (varchar) for the case that the database is not mysql. 'pubblish_date' => array( 'description' => t('The pubblish date for the single news'), 'type' => 'varchar', 'mysql_type' => 'datetime', 'not null' => FALSE, ), 'unpublish_date' => array( 'description' => t('The unpublish date for the ...


12

The way to do this is to use hook_flush_caches in combination with register_shutdown_function. Example code: /** * Implements hook_flush_caches(). */ function mymodule_flush_caches() { // After caches are cleared we will run mymodule_cache_rebuild() register_shutdown_function('mymodule_cache_rebuild'); // We don't want to add any custom cache-...


12

Like this: if ($node->isNew()) { } Note that this only works before saving, in insert/update, it will always return TRUE, because the node has already been saved and is no longer new.


11

If you want to theme the outgoing mails with the help of custom templates, you should use the HTML Mail module. Lets you theme your messages the same way you theme the rest of your website. It works well with other modules, like Print, Mail mime, etc. You have to install the Mail System module in order to use HTML mail.


11

The trick, apparently, is to use the system module like: dependencies[] = system (>=7.17)


11

You can achieve your goal without jQuery. Just use theme_menu_link() function, or (my suggestion) theme_menu_link__your_menu() Place this function in your template.php file: function YOURTHEME_menu_link(array $variables) { $element = $variables['element']; $sub_menu = ''; if ($element['#below']) { $sub_menu = drupal_render($element['#...


11

Goto the Manage Display page for each content type (eg, admin/structure/types/manage/page/display). Click on the tab for Custom Display Settings. You will see the view modes that your system knows about. Tick Search Index and save. You can now configure which fields get indexed from the tab for that new display mode (eg admin/structure/types/manage/page/...


11

No, there isn't. Not really. At least not in 6 or 7. Assuming 7: If you will look at drupal_flush_all_caches() you will see it invokes hook_flush_caches(). This hook is meant to: "add cache table names to the list of cache tables that will be cleared by the Clear button on the Performance page or whenever drupal_flush_all_caches is invoked." It ...


11

It seems that it's impossible using only Drupal, which means: variable_set() itself doesn't invoke any hooks, but it uses db_merge(). That function is using the MergeQuery class. Now, it would be nice to hook with hook_query_alter(), but it only works for query classes that implements the QueryAlterableInterface interface. Sadly, this interface is now ...


11

Yes, you can implement any alter hook in a theme, as well as hook_theme() and preprocess hooks. Adding $form_state['redirect'] won't do anything there, though. You need to do that in a submit handler: function MYTHEME_form_phone_book_node_form_alter(&$form, &$form_state, $form_id) { $form['#submit'][] = 'MYTHEME_phone_book_node_form_submit'; } ...


11

When I did this a while ago I adapted the excellent field_example module from the Examples Module which is always the first place to look, as it has a lot of Drupal best practice in it. As they say in the module comments: * Providing a field requires: * - Defining a field: * - hook_field_info() * - hook_field_schema() * - ...


11

Generally speaking, only alter hooks can be implemented by themes, which means hooks like hook_form_alter() and hook_menu_alter(), or in short all those hooks that are invoked through drupal_alter() in Drupal 7 and lower (ModuleHandler()::alter() or ThemeManager::alter() in Drupal 8). Other hooks, which are invoked by module_invoke_all() (ModuleHandler::...


11

An .api.php file contains the hooks that a certain module supports, and is available for reference. It offers documentation and example code on how to use the hooks in other modules. It is not specific to Drupal 8 as several modules in Drupal 7 offer it as well. It does not actually implement or call the hooks and is for reference only. As for an example ...


Only top voted, non community-wiki answers of a minimum length are eligible