Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: Logic hook that will allow adding custom javascript code in header without modifying template #7158

Open
gboban opened this issue Apr 8, 2019 · 5 comments
Labels
Type:Suggestion Issue containing a suggestion of functionality, process or UI. Associated PRs are called enhancement

Comments

@gboban
Copy link

gboban commented Apr 8, 2019

Issue

Add logic hoot that will allow inserting custom javascript (or other) code in page header.

Currently, after_ui_frame, after_ui_footer and server_round_trip logic hooks add code towarde the end of the page and after_entry_point will add code on the top of the page, before any other processing.

For example, Google Analytis code should be placed at the top of the head tag and that requires modifying theme template (themes//tpls/_head.tpl)

@willrennie willrennie added the Type:Suggestion Issue containing a suggestion of functionality, process or UI. Associated PRs are called enhancement label Apr 8, 2019
@pstevens71
Copy link
Contributor

I know this is an old post, but in case anyone else comes across this, you absolutely can add JS to head of editview without modifying the template. Here's an example ( I have a custom module billable hours where I want to calculate the total charge based on hours x hourly rate). Also, this is an application hook. You have to put it in custom/modules/logic_hooks.php

`$hook_version = 1;
$hook_array = array();

$hook_array['after_ui_frame'] = array();
$hook_array['after_ui_frame'][] = array(
1, // Processing order
'add_custom_js_to_editview', // Hook method
'custom/modules/bill_billable_hours/add_custom_js_to_editview.php', // Custom PHP file
'CustomJS', // Class name (if needed)
'addCustomJsToEditView' // Method name
);`

Then here's my PHP file:
`<?php
if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

class CustomJS {
public function addCustomJsToEditView($event, $arguments) {
if ($event == 'after_ui_frame') {
global $js_strings;
// limit to only the module you want it to fire on
if (!empty($_REQUEST['module']) && $_REQUEST['module'] == 'bill_billable_hours') {
$js = <<<EOT

<script type="text/javascript"> // Custom JavaScript code $(document).ready(function() { // Function to update total_billing based on bill_rate and hours function updateTotalBilling() { var billRate = parseFloat($('input[name="bill_rate"]').val()); var hours = parseFloat($('#hours').val()); if (!isNaN(billRate) && !isNaN(hours)) { var totalBilling = billRate * hours; $('#total_billing').text(totalBilling.toFixed(2)); } } // Attach event listeners to bill_rate and hours fields $('input[name="bill_rate"], #hours').on('input', updateTotalBilling); // Initialize total_billing field updateTotalBilling(); }); </script>

EOT;
echo $js;
}
}
}
}`

@chris001
Copy link
Contributor

chris001 commented Aug 14, 2023

Same way for adding custom CSS to the head of all pages, not just editview, without modifying template, so it is for all templates?

@pstevens71
Copy link
Contributor

pstevens71 commented Aug 14, 2023 via email

@pstevens71
Copy link
Contributor

Actually, on further investigation it places the script close to the beginning of the body not in the . However, the functionality works of adding in JS to the page without having to edit the template. For CSS this would be a good place to put it too because it would override CSS in the head because it loads later. You just have to get creative about how you limit where it fires because the hook will fire on every page and you need limit the function to only output the JS or CSS on the pages you want.

@chris001
Copy link
Contributor

@pstevens71 Thanks. Tried it here. You're right. It works well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type:Suggestion Issue containing a suggestion of functionality, process or UI. Associated PRs are called enhancement
Projects
None yet
Development

No branches or pull requests

4 participants