1

So, to set the stage, I'm working on a small database project that will handle a company's order handling. So far, I have a fully working client part where users can get a full list of all the clients and can edit/delete/add clients at will.

The next think that I'm trying to implement, is that each client will also have it's own details page, where more details of the client will be displayed. It should display all the data in the database regarding the specific client, but it should also incorporate a Google Map based on the client's address.

I've looked around and found a dozen of Google Maps modules, but they only seem to work with article types that have fields that contain an address.

My current line of thinking was just to render the detail page with a render array after getting all the client's info from my database.

1) Is this the correct way of doing it? If not, how should I approach this?

2) If yes, how can I then implement a google map?

EDIT:

The thing is, I'm not exactly working with a content type at the moment. I'm using hook_menu for all my pages, and an example callback function for displaying a full list of all the clients is

function expoline_clients_overview() {
    // Table settings.
  $attributes['datatable_options'] = array(
    'bFilter'   => TRUE,
    'bInfo'     => TRUE,
  );
  // Define table columns
  $header = array(
    array(
      'data' => t('Column 1'),
      'datatable_options' => array(
        'bSortable' => TRUE,
        'bSearchable' => TRUE,
      ),
    ),
    array(
      'data' => t('Column 2'),
      'datatable_options' => array(
        'bSortable' => TRUE,
        'bSearchable' => TRUE,
      ),
    ),
    array(
      'data' => t('Column 2'),
      'datatable_options' => array(
        'bSortable' => TRUE,
        'bSearchable' => FALSE,
      ),
    ),
    array(
      'data' => t('Column 2'),
      'datatable_options' => array(
        'bSortable' => TRUE,
        'bSearchable' => TRUE,
      ),
    ),
    array(
      'data' => t('Column 2'),
      'datatable_options' => array(
        'bSortable' => TRUE,
        'bSearchable' => TRUE,
      ),
    ),
    array(
      'data' => t('Column 2'),
      'datatable_options' => array(
        'bSortable' => TRUE,
        'bSearchable' => TRUE,
      ),
    ),
    array(
      'data' => t('Column 2'),
      'datatable_options' => array(
        'bSortable' => TRUE,
        'bSearchable' => TRUE,
      ),
    ),
  );

  /*
   * Fetch all clients from the database and insert them into the array of rows
   * that will be rendered in the table
  */
  $query = db_query("SELECT * FROM {expoline_clients}");
  foreach ($query as $currentclient) {
    unset($client);
    $client[] = $currentclient->name;
    $client[] = $currentclient->street;
    $client[] = $currentclient->street_number;
    $client[] = $currentclient->postal_code;
    $client[] = $currentclient->city;
    $client[] = '<a href="clients/' . $currentclient->cid . '/edit"><button type="button">Edit</button></a>';
    $client[] = '<a href="clients/' . $currentclient->cid . '/delete"><button type="button">Delete</button></a>';
    $clients[] = $client;
  }
  $rows = $clients;

  // Render the Datatable
  $variables = array(
    'attributes' => $attributes,
    'header' => $header,
    'rows' => $rows,
  );
  $output = theme('datatable', $variables);

  return $output . '<a href="clients/add"><button type="button">' . t('Klant toevoegen') . '</button></a>';
}

Thanks a bunch!

  • You're making an external database with details of clients and outputting it using Drupal? – Beebee Jan 30 '15 at 16:03
  • Nope, all the data is stored in a table in drupal's database. – Neglexis Jan 30 '15 at 16:04
  • I don't fully understand, "they only seem to work with article types that have fields that contain an address." indicates to me that you're not using Drupal's Entities or Node system, so what's feeding data to this table? How are you outputting it currently? Using Views? Custom module with hook/menu? – Beebee Jan 30 '15 at 16:07
  • @Beebee Indeed, it's just a custom module with hook_menu, see my edit. It could totally be that I'm handling things in a totally incorrect way (although I'm having the intended results). If there is a more Drupal'y way of doing things like this, please let me know. It's my first time writing a fully custom module, so all feedback is appreciated! – Neglexis Jan 30 '15 at 16:58
1

I didn't dig into the exact methods you are using, but if all you want to do is embed a very simple map graphic you could use something like this:

if (isset($currentclient->street_number) && isset($currentclient->street)) {
      $address_var = $currentclient->street_number . " " . $currentclient->street;
      if (isset($currentclient->city)) $address_var .= "+".$currentclient->city;
      if (isset($currentclient->state)) $address_var .= "+".$currentclient->state;
      if (isset($currentclient->postal_code)) $address_var .= "+".$currentclient->postal_code;
      $address_var = str_replace(' ', '+', $address_var);
      $output = "<a href='https://maps.google.com?daddr=".$address_var."'><img style='float: right; ' src='https://maps.googleapis.com/maps/api/staticmap?center=".$address_var."&zoom=15&size=300x150
&markers=color:blue%7Clabel:*%7C".$address_var."' /></a>";
    }

Or, I suppose a render array would simply change the $output= line above to be something like this:

$output =  array(
    'map' => array(
      '#type' => 'markup',
      '#markup' => '<a href="https://maps.google.com?daddr='.$address_var.'"><img  src="https://maps.googleapis.com/maps/api/staticmap?center='.$address_var.'&zoom=15&size=300x150
    &markers=color:blue%7Clabel:*%7C'.$address_var.'" /></a>',
    ),
);
  • Thanks @Laryn. But as I said in another comment, I know I could just output it all in native HTML, but seeing as apparently outputting pure HTML is the D6 way of doing it, I was wondering what the D7 renderable array way of doing it would be. – Neglexis Jan 30 '15 at 20:31
  • Fair enough. Not sure if the edit above is useful or not but I think you can basically feed it in to a render array as markup. – Laryn - CEDC.org Jan 30 '15 at 20:57
0

OK, My suggestion is you can create a Full HTML text in your content type and get your client's address (postcode or address). Then render values form DB with url from google to generate an embed map iframe, then assign the iframe's mark up to your FULL HTML text field. Please refer to Google Developer API to find out how to generate embed map. You can use hook_node_presave or hook_form_alter to do this. It's seem your thinking is in right direction.

  • The thing is that I'm not using a content type for my module. See my edit in my question. – Neglexis Jan 30 '15 at 16:56
  • Then I think you need to design the whole architecture again, because Drupal is all about field and content type. Yes, the menu_hook will allow you to call function and display result on page, but it is definitely should not be used in your context to query db and display client's detail on the page. It is a long explanation why you should not use hook_menu this way, but I think you need to create client as content type and store details there OR "generate node of Client content type from DB". – user1139747 Jan 30 '15 at 17:53
  • @user1139747 That's not true at all I'm afraid - content types aren't suitable for everything, and they come with quite a performance cost. Nothing wrong with managing your data manually; arguably if you want to make it more Drupal'y, create a custom entity for the job, but many, many of everyone's favourite modules manage their data outside of Drupal's clutches (e.g. Views). If the data the OP needs to display is in a custom table, then they most certainly should be using a page callback to query the database and display the results. It's the whole point of the API. – Clive Jan 30 '15 at 18:00
  • But for balance I should also say that creating a Client content type would also be a good solution to this, no arguments there. My point is that there are options, deliberately, and one is not necessarily better than the other by default – Clive Jan 30 '15 at 18:04
  • @Clive, Yes sure... I do agree with you about using entity to manage you custom data and table or views to display these data. As this approach is also standard, but not agree to pull data purely using menu_hook and then print out data, because the data is not Content manageable. – user1139747 Jan 30 '15 at 18:21

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.