0

I want to create a new button control in my ip_geoloc leaflet map. So, i created a view by using the drupal interface -> /admin/structure/views/view/markers_admin/edit

And now i want to set a custom JS wich execute only in this view. I've create a module to create custom form. And my goals are when the user click on the button control in the map, a modal pop with a form with lat/lng field to add dynamically the marker on the map.

I searched everything (how to create a modal -> colorbox, how to create a custom button, how to add the marker...) but i don't know how to attach the javascript to this page.

I've tried to implements these hooks (4th comments) in my custom module but this not work : https://www.drupal.org/node/1993228

Can you help me ?

0

If you are following that 4th comment exactly the problem is probably that it is a hook for viewing a node, not for a 'view'. Ambiguous to be sure.

The accepted answer here should be helpful: How do I add JavaScript specific for a view?.

EDIT

Okay, this may be more helpful. The trick here is finding a place to hook in to check to the page before it is rendered to add the javascript to the render array. The assumption this code makes (which may not be accurate) is that you are looking at a views page. If the view is nested in a block or a panels pane this may not work as well. Here goes:

/**
 * Implements hook_page_alter().
 */
function food_truck_form_page_alter(&$page) {
  $views_page = views_get_page_view();

  if ($views_page->name == 'marqueurs_admin') {
    $path = drupal_get_path('module', 'food_truck_form' );
    $page['#attached']['js'][] = array(
      'type'=>'file',
      'data' => $path . 'js/food_truck_form.js'
    );
  }
}

This also assumes that you are implementing it in a module called food_truck_form and your view is named 'marqueurs_admin'.

Also, as a side-note, it often works better formatting-wise to edit your post to add the additional info there rather than in the comments.

| improve this answer | |
  • i've code this function food_truck_form_views_pre_render(&$view) { // check to make sure the view has a classname if ($view->display_handler && ! empty ( $view->display_handler->options ['css_class'] )) { $cln = $view->display_handler->options ['css_class']; $cls = 'view-marqueurs-admin'; // test that the classname contains our class if (preg_match ( '/(^|\s+)' . preg_quote ( $cls ) . '(\s+|$)/i', $cln )) { drupal_add_js ( drupal_get_path ( 'modules', 'food_truck_form' ) . 'js/food_truck_form.js' ); } } } but this isn't work :/ ps sorry for the lack of indent – Kpone Apr 25 '16 at 8:25
  • and i try this : function food_truck_form_views_pre_render(&$view) { if ($view->name == 'marqueurs_admin') { drupal_add_js(drupal_get_path('modules', 'food_truck_form') . 'js/food_truck_form.js'); } } and this : function THEME_preprocess_views_view(&$vars) { $view = &$vars['view']; if ($view->name == 'marqueurs_admin') { drupal_add_js(drupal_get_path('modules', 'food_truck_form') . 'js/food_truck_form.js'); } } – Kpone Apr 25 '16 at 8:34
  • I have updated my suggestion. If the assumptions made are wrong - chiefly where this view is being used - let me know and we'll find a different place to hook in. – greendemiurge Apr 25 '16 at 15:51
  • IDK why but this not work :/ it's strange... i access to this url localhost/drupal/marqueurs-admin wich is the 'marqueurs_admin' view (it's the right name, i do a var_dump(views_get_page_view()->name) in my view and it show the right machine_name. In all hooks if i do var_dump(views_get_page_view()->name) it show nothing :/ it's a bit strange... – Kpone Apr 26 '16 at 8:21
  • Maybe i solve the problem... api.drupal.org/api/drupal/modules%21system%21system.api.php/… first comment : Refresh cache... thanks, every solutions works correctly when i refresh the cache ;) – Kpone Apr 26 '16 at 8:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.