Drupal add JS from the UI

Drupal add JS from the UI

http://wilsonrelocation.com/?q=%D8%A7%D9%86%D8%B3%D8%AA%D8%A7-%D9%81%D9%88%D8%B1%D9%83%D8%B3 Adding JavaScrip to Drupal pages is super simple as you know but it you would prefer not to have to do a full release of the site in question for the sake of a new banner or whet ever amazing and critical feature you are adding on behalf of your marketing department the options are limited. 

كم سعر كيلو الذهب

تعليم على مؤشرات الأسهم You could use a block, this is a reasonably painless way as the blocks interface has a nice way to target pages specifically and in most cases it works, it is however pretty nasty when you think about it, there is also limited control of where in the page you place this JavaScript, it's not like you can put a block in the <head> of your page is it. 

الحساب الافتراضي خيار ثنائي

اسم نطاقك There may be other solutions for this problem but I was suprised recently to find nothing really useful or tailored to provide this functionality in a clean and user friendly way in either Drupal 6 or Drupal 7. 

اكتشاف هنا

بوب إلى هذا الموقع Below is a code for the majority of the Drupal 7 module I have made to do this for me, to get the whole thing go to my GitHub where you can find either a Drupal 6 or Drupal 7 version that both use the respective logic from the blocks module to target specific pages/paths. 

المتاجرة بسوق الذهب

سعر الاسهم اليوم مباشر If you see any way to improve either one please send me a pull request, I would be grateful and it could help someone else. 

http://www.tyromar.at/?yuwlja=%D8%A7%D9%84%D9%85%D8%AA%D8%A7%D8%AC%D8%B1%D9%87-%D8%A8%D8%A7%D9%84%D8%B0%D9%87%D8%A8&434=fe

http://asect.org.uk/?ilyminaciya=%D8%A7%D9%86%D8%B3%D8%AD%D8%A7%D8%A8-%D8%A7%D9%84%D8%B1%D9%88%D8%A8%D9%88%D8%AA-%D8%AE%D9%8A%D8%A7%D8%B1-%D8%AB%D9%86%D8%A7%D8%A6%D9%8A&b7e=9a <?php /** * @file * Provides a UI for admin users to add JavaScript to pages without needing * either custom code or a deployment. */   define('ADD_JS_USE_ON_ALL', 1); define('ADD_JS_USE_ON_ALL_EXCEPT', 2); define('ADD_JS_USE_ONLY_ON', 3); define('ADD_JS_LIST_PATH', 'admin/config/add-js'); define('ADD_JS_ADD_PATH', 'admin/config/add-js/add');   /** * Returns the textual explaination of the usage option * * @param int $value * * @return string */ function add_js_usage_option_text($value) { switch ($value) { case 1: return t('Use on all pages'); case 2: return t('Use on all pages except those specified'); case 3: return t('Only use on specified pages'); } }   /** * Implements hook_preprocess_page * * Adds JavaScripts from the database to a page if conditions are met * * @param array $variables */ function add_js_preprocess_page(&$variables) { $query = db_select('add_js', 'js'); $query->fields('js', array('id', 'title', 'usage_option', 'scope', 'weight', 'javascript', 'paths')); $result = $query->orderBy('js.id')->execute();   foreach ($result as $row) { // Make a comme cnt for the JS to make it easier to identify in the source $comment = "//Add js: $row->title \n"; switch ($row->usage_option) { case ADD_JS_USE_ON_ALL : drupal_add_js( $comment . $row->javascript, array( 'type' => 'inline', 'scope' => $row->scope, 'weight' => $row->weight, ) ); break; case ADD_JS_USE_ON_ALL_EXCEPT : $page_match = add_js_match_paths_to_current_path($row->paths); if ($page_match === FALSE) { drupal_add_js( $comment . $row->javascript, array( 'type' => 'inline', 'scope' => $row->scope, 'weight' => $row->weight, ) ); } break; case ADD_JS_USE_ONLY_ON : $page_match = add_js_match_paths_to_current_path($row->paths); if ($page_match === TRUE) { drupal_add_js( $comment . $row->javascript, array( 'type' => 'inline', 'scope' => $row->scope, 'weight' => $row->weight, ) ); } break; } } }   /** * Matches the current path to the paths on this javascript entry * Functionlay similar to how blocks works * * @param string $paths * * @return bool */ function add_js_match_paths_to_current_path($paths) { if (!empty($paths)) { $pages = drupal_strtolower($paths); $path = drupal_strtolower(drupal_get_path_alias($_GET['q'])); $page_match = drupal_match_path($path, $pages); if ($path != $_GET['q']) { $page_match = $page_match || drupal_match_path($_GET['q'], $pages); } } else { $page_match = TRUE; }   return (bool) $page_match; }   /** * Implements hook_menu() * * @return array $items */ function add_js_menu() { $items = array();   // Default config $items[ADD_JS_LIST_PATH] = array( 'title' => t('Add JS'), 'page callback' => 'add_js_list', 'access arguments' => array('administer add js'), 'type' => MENU_NORMAL_ITEM, ); $items[ADD_JS_ADD_PATH] = array( 'title' => t('Add New JavaScript to pages'), 'page callback' => 'drupal_get_form', 'page arguments' => array('add_js_add_new_js_form'), 'access arguments' => array('administer add js'), 'type' => MENU_NORMAL_ITEM, ); $items['admin/config/add-js/edit/%'] = array( 'title' => 'Edit JS', 'title callback' => 'add_js_title_callback', 'title arguments' => array(4, t('Edit')), 'page callback' => 'drupal_get_form', 'page arguments' => array('add_js_edit_existing_js_form', 4), 'access arguments' => array('administer add js'), 'type' => MENU_CALLBACK, ); $items['admin/config/add-js/delete/%'] = array( 'title' => 'Delete JS', 'title callback' => 'add_js_title_callback', 'title arguments' => array(4, t('Delete')), 'page callback' => 'drupal_get_form', 'page arguments' => array('add_js_delete_existing_js_form', 4), 'access arguments' => array('administer add js'), 'type' => MENU_CALLBACK, );   return $items; }   /** * Implementation of hook_permission(). */ function add_js_permission() { $permissions['administer add js'] = array( 'title' => t('Administer Add JS Module'), ); return $permissions; }   /** * Title callback for existing JavaScript items * * @param int $js_id * @param string $action * Whether this is the Edit or Delete page * * @return string */ function add_js_title_callback($js_id, $action) { $query = db_select('add_js', 'js'); $query->addField('js', 'title'); $title = $query->execute()->fetchField();   return t( '!action JavaScript: !title', array( '!action' => $action, '!title' => $title ) ); }   /** * Page callback for the list of current JavaScripts * * @return string */ function add_js_list() { // Add a link to the Add new JS page $link = '<p>' . l(t('Add new JavaScript'), ADD_JS_ADD_PATH) . '</p>';   // Build the table $header = _add_js_retrieve_list_header(); $rows = _add_js_retrieve_list_rows();   // Create a render array ($build) which will be themed as a table with a // pager. $build['pager_table'] = array( '#prefix' => $link, '#theme' => 'table', '#header' => $header, '#rows' => $rows, '#empty' => t('There are no JavaScripts found in the db'), );   // Attach the pager theme. $build['pager_pager'] = array('#theme' => 'pager');   return $build; }   /** * Returns the table headings array for the list page * * @return array */ function _add_js_retrieve_list_header() { $header = array( array('data' => t('Title')), array('data' => t('Usage Option')), array('data' => t('Scope')), array('data' => t('Weight')), array('data' => t('Edit')), array('data' => t('Delete')), );   return $header; }   /** * Returns the table rows array for the list page * * @return array */ function _add_js_retrieve_list_rows() { $query = db_select('add_js', 'js')->extend('PagerDefault'); $query->fields('js', array('id', 'title', 'usage_option', 'scope', 'weight')); $result = $query->limit(50)->orderBy('js.id')->execute();   $rows = array(); foreach ($result as $row) { $title = $row->title; $option = add_js_usage_option_text($row->usage_option); $scope = $row->scope; $weight = $row->weight; $edit = l('Edit', 'admin/config/add-js/edit/' . $row->id); $delete = l('Delete', 'admin/config/add-js/delete/' . $row->id);   $rows[] = array($title, $option, $scope, $weight, $edit, $delete); }   return $rows; }   /** * Implements hook_form * * This is the form for adding new JavaScripts to pages * * @param array $form_state * * @return array */ function add_js_add_new_js_form($form, &$form_state) { $form = array();   $form['title'] = array( '#type' => 'textfield', '#title' => t('Title'), '#description' => t('A unique name for this JavaScript'), '#default_value' => NULL, '#required' => TRUE, );   $form['javascript'] = array( '#type' => 'textarea', '#title' => t('JavaScript'), '#description' => t('Do not include the opening and closing script tags'), '#default_value' => NULL, '#required' => TRUE, );   // Add radio to choose the scope option $form['scope'] = array( '#type' => 'radios', '#title' => t('Scope'), '#description' => t('Where should this JavaScript be placed within ' . 'the source code'), '#options' => array( 'footer' => 'Footer', 'header' => 'Header', ), '#default_value' => 'footer', '#required' => TRUE, );   $form['weight'] = array( '#type' => 'textfield', '#title' => t('Weight'), '#description' => t('Optional numeric weight value used in drupal_add_js'), '#default_value' => NULL, '#required' => FALSE, );   // Add radio to choose usage option $form['usage_option'] = array( '#type' => 'radios', '#title' => t('Usage Option'), '#description' => t('Where should this JavaScript be used'), '#options' => array( ADD_JS_USE_ON_ALL => add_js_usage_option_text( ADD_JS_USE_ON_ALL ), ADD_JS_USE_ON_ALL_EXCEPT => add_js_usage_option_text( ADD_JS_USE_ON_ALL_EXCEPT ), ADD_JS_USE_ONLY_ON => add_js_usage_option_text( ADD_JS_USE_ONLY_ON ), ), '#default_value' => ADD_JS_USE_ON_ALL, );   $form['paths'] = array( '#type' => 'textarea', '#title' => t('Paths to match against the usage option'), '#description' => t('Use Drupal paths, one per line'), '#default_value' => NULL, '#required' => FALSE, );   $form['submit'] = array( '#type' => 'submit', '#value' => t('Save JavaScript') );   return $form; }   /** * Implements hook_form_validate * * This is the validator for the add_js_add_new_js_form form * * @param array $form * @param array $form_state */ function add_js_add_new_js_form_validate(&$form, &$form_state) { // The title and JavaScript fields are mandatory so are already taken care of // The only other validation is on the Usage Options, Weight & Paths combination // If this is for use on specified pages only make sure the paths // value is not empty if ( $form_state['values']['usage_option'] == ADD_JS_USE_ON_ALL_EXCEPT || $form_state['values']['usage_option'] == ADD_JS_USE_ONLY_ON ) { if (mb_strlen(trim($form_state['values']['paths'])) == 0) { form_set_error('paths', t('If this is for use on specified pages only ' . 'make sure the paths textarea is not empty')); } }   if (!empty($form_state['values']['weight'])) { if (!is_numeric($form_state['values']['weight'])) { form_set_error('weight', t('The weight value must be numeric.')); } } }   /** * Implements hook_form_submit * * This is the submit handler for the add_js_add_new_js_form form * * @param array $form * @param array $form_state */ function add_js_add_new_js_form_submit(&$form, &$form_state) { // Add a new row to the add_js table $title = $form_state['values']['title']; $javascript = $form_state['values']['javascript']; $scope = $form_state['values']['scope']; $weight = $form_state['values']['weight']; $option = $form_state['values']['usage_option']; $paths = $form_state['values']['paths'];   db_insert('add_js') ->fields(array( 'title' => $title, 'usage_option' => $option, 'scope' => $scope, 'weight' => $weight, 'javascript' => $javascript, 'paths' => $paths, )) ->execute();   drupal_goto(ADD_JS_LIST_PATH); }   /** * Implements hook_form * * This is the form for editing existing JavaScripts * * @param array $form_state * @param int $id * The id of the JS to edit * * @return array $form */ function add_js_edit_existing_js_form($form, &$form_state, $id) {   // put the ID in storage for the Validate and Submit handlers $form_state['storage']['add_js_id'] = $id;   $query = db_select('add_js', 'js'); $query->fields('js', array('id', 'title', 'usage_option', 'scope', 'weight', 'javascript', 'paths')); $existing = $query->condition('id', $id)->execute()->fetchAssoc();   $form = array();   $form['title'] = array( '#type' => 'textfield', '#title' => t('Title'), '#description' => t('A unique name for this JavaScript'), '#default_value' => $existing['title'], '#required' => TRUE, );   $form['javascript'] = array( '#type' => 'textarea', '#title' => t('JavaScript'), '#description' => t('Do not include the opening and closing script tags'), '#default_value' => $existing['javascript'], '#required' => TRUE, );   // Add radio to choose the scope option $form['scope'] = array( '#type' => 'radios', '#title' => t('Scope'), '#description' => t('Where should this JavaScript be placed within ' . 'the source code'), '#options' => array( 'footer' => 'Footer', 'header' => 'Header', ), '#default_value' => $existing['scope'], '#required' => TRUE, );   $form['weight'] = array( '#type' => 'textfield', '#title' => t('Weight'), '#description' => t('Optional numeric weight value used in drupal_add_js'), '#default_value' => $existing['weight'], '#required' => FALSE, );   // Add radio to choose usage option $form['usage_option'] = array( '#type' => 'radios', '#title' => t('Usage Option'), '#description' => t('Where should this JavaScript be used'), '#options' => array( ADD_JS_USE_ON_ALL => add_js_usage_option_text( ADD_JS_USE_ON_ALL ), ADD_JS_USE_ON_ALL_EXCEPT => add_js_usage_option_text( ADD_JS_USE_ON_ALL_EXCEPT ), ADD_JS_USE_ONLY_ON => add_js_usage_option_text( ADD_JS_USE_ONLY_ON ), ), '#default_value' => $existing['usage_option'], );   $form['paths'] = array( '#type' => 'textarea', '#title' => t('Paths to match against the usage option'), '#description' => t('Use Drupal paths, one per line'), '#default_value' => $existing['paths'], '#required' => FALSE, );   $form['submit'] = array( '#type' => 'submit', '#value' => t('Save Edited JavaScript') );   // Use the same validator as the add new form as we are validating the same data $form['#validate'][] = 'add_js_add_new_js_form_validate';   return $form; }   /** * Implements hook_form_submit * * This is the submit handler for the add_js_edit_existing_js_form form * * @param array $form * @param array $form_state */ function add_js_edit_existing_js_form_submit(&$form, &$form_state) { // Add a new row to the add_js table $id = $form_state['storage']['add_js_id']; $title = $form_state['values']['title']; $javascript = $form_state['values']['javascript']; $option = $form_state['values']['usage_option']; $scope = $form_state['values']['scope']; $weight = $form_state['values']['weight']; $paths = $form_state['values']['paths'];   db_update('add_js') ->fields(array( 'title' => $title, 'javascript' => $javascript, 'usage_option' => $option, 'scope' => $scope, 'weight' => $weight, 'paths' => $paths, )) ->condition('id', $id) ->execute();   drupal_goto(ADD_JS_LIST_PATH); }   /** * Implements hook_form * * This is the form for deleting existing JavaScripts * * @param array $form_state * @param int $id * The id of the JS to edit * * @return array */ function add_js_delete_existing_js_form($form, &$form_state, $id) {   $form_state['storage']['add_js_id'] = $id;   $query = db_select('add_js', 'js'); $query->addField('js', 'title'); $existing = $query->condition('id', $id)->execute()->fetchAssoc();   $form = array(); $question = t('Are you sure you want to delete: ' . $existing['title']); $path = ADD_JS_LIST_PATH; $description = t('Pressing the delete button will permenantly delete the ' . 'JS: ' . $existing['title']); $yes = t('Delete ' . $existing['title'] . ' permenantly'); $no = t('Go back to the add js list page');   // Return a standard return form return confirm_form($form, $question, $path, $description, $yes, $no); }   /** * Implements hook_form_submit * * Does the actual deleting of the js entry * * @param array $form * @param array $form_state */ function add_js_delete_existing_js_form_submit(&$form, &$form_state) { $id = $form_state['storage']['add_js_id'];   db_delete('add_js') ->condition('id', $id) ->execute();   drupal_goto(ADD_JS_LIST_PATH); }

Comment

By submitting this form, you accept the Mollom privacy policy.