Friday, 7 October 2016

JQuery on a form that sends to submit once all the fields are filled out


                              // All sections have been filled out
                // After this we'll then reveal the next section.

                $("#edit-field-supplier-email-und-0-email").focusout(function(e){
                    // has all fields got data in.  If so lets move on
                    if ( $("#edit-field-supplier-email-und-0-email").val() != '' && $("#edit-field-company-name-und-0-value").val() != '' && $("#edit-field-phone-number-und-0-value").val() != '' && $("#edit-field-post-code-und-0-value").val() != '' && $("#edit-field-supplier-email-und-0-email").val() != ''){

                        // prefill postion placing
                        $("#edit-field-position-reached-und-0-value").val(1);

                        $("#supplier-node-form").submit();

                    }

                });


Wednesday, 7 September 2016

JQuery to check a Checkbox is filled out and then add a class to the parent Div.

I had a bit of a struggle with this simple bit of code. And that was because examples I'd found hadn't include the use of .each !!

Here's a couple of searches I'd made - 'javascript checkbox value checked then add class to parent' , 'jquery onload' and 'jquery check an object with no action'

The code that worked for me is as such

 

$("input[type=checkbox], input[type=radio]").each(function(){
                    console.log('loading checkbox object');
                    // is(':checked')
                    if($(this).is(':checked')){
                        $(this).parent().addClass("chked");
                        $(this).parent().parent().addClass("chked");
                    }
                });

Thursday, 1 September 2016

Drupal 7 Form API displaying only the Parent options in an Entity / Content Field item

So what I have is a Content Form where I want to display a Term / Taxonomy but only the Parent items for the moment.

Here's a way of solving this programmatically in a Hook Form Alter.

In my hook_form_FORM_ID_alter I iterate through the options array and remove the child choices. This is easy as those choices start with a minus sign .

  foreach ($form['my_field']['und']['#options'] as $key => $value) {
   // if $value starts with '-' then its a child item and I'll unset it.
    if ($value[0] == '-'){
      unset($form['my_field']['und']['#options'][$key]);
    }

  }

Tuesday, 2 August 2016

OpenOffice and how to count Yes's in a column ( or any string )

Click on Sum and use something like this

=COUNTIF(A2:A100000;"=yes")

Tuesday, 26 July 2016

Drupal 7 - Extending Push Notification Extended

The following is an extension to Push Notifications module in Drupal 7 to pass through extra data with the Payload.

This module is currently for testing purposes and can be called through the URL call to notifications/send_extend/%

Where % is the User Id of the user we'll be sending the notification to.

The change to the Payload array is in push_notifications_send_alert_extended .

Super Administration and Administration are the only roles allowed to visit the above link.

Module -> push_notifications_extend

push_notifications_extend.info

name = Push Notifications Extend
description = A module for extending the Push Notifications Module
core = 7.x
dependencies[] = push_notifications


push_notifications_extend.module

 'Send APNS Notifications',
    'page callback' => 'push_notifications_extend_manual_send',
    'access callback' => 'push_notifications_extend_send_access',
    'type' => MENU_CALLBACK,
  );

  return $items;
}


function push_notifications_extend_manual_send() {

  $user_to_send_to = arg(2);

  $users_to_notify[] = $user_to_send_to;

  $return = "

Sending push notification to " . count($users_to_notify) . " user.

"; push_notifications_extend_send_message($users_to_notify); variable_set("user_queue", 0); variable_set("apns_sent", 0); return $return; } function push_notifications_extend_send_message($users) { $unix_timestamp = microtime(); $watchdog_msg = 'Sending test message to ' . count($users) . ' user - at ' . $unix_timestamp; $message = "Test - Log Ref : $unix_timestamp"; push_notifications_send_message_extended($users, $message); } /** * Access callback for users to send APNS. * * @return bool */ function push_notifications_extend_send_access() { global $user; $access = FALSE; if (in_array('Super Admin', $user->roles) || in_array('administrator', $user->roles)) { $access = TRUE; } return $access; } /** * Send a simple message alert to an array of recipients. * * @param array $recipients Array of user ids. * @param string $message Message to be included in payload. * @return mixed Flag to indicate if delivery was successful. */ function push_notifications_send_message_extended($recipients, $message) { // Shorten the message characters / 8 bit. $message = truncate_utf8($message, PUSH_NOTIFICATIONS_APNS_PAYLOAD_SIZE_LIMIT, TRUE, TRUE); // Convert the payload into the correct format for delivery. $payload = array('alert' => $message); $tokens = array(); foreach ($recipients as $uid) { $user_tokens = push_notification_get_user_tokens($uid); if (!empty($user_tokens)) { $tokens = array_merge($tokens, $user_tokens); } } // Stop right here if none of these users have any tokens. if (empty($tokens)) { return FALSE; } // Send a simple alert message. push_notifications_send_alert_extended($tokens, $payload); } /** * Handle delivery of simple alert message. * * @param array $tokens Array of token record objects. * @param array $payload Payload. * */ function push_notifications_send_alert_extended($tokens = array(), $payload = array()) { // Group tokens into types. $tokens_ios = array(); $tokens_android = array(); foreach ($tokens as $token) { switch ($token->type) { case PUSH_NOTIFICATIONS_TYPE_ID_IOS: $tokens_ios[] = $token->token; break; case PUSH_NOTIFICATIONS_TYPE_ID_ANDROID: $tokens_android[] = $token->token; break; } } // Send payload to iOS recipients. if (!empty($tokens_ios)) { // Convert the payload into the correct format for APNS. $payload_apns = array('aps' => $payload, 'acme' => 'foo'); push_notifications_apns_send_message($tokens_ios, $payload_apns); } // Send payload to Android recipients if configured correctly. if (!empty($tokens_android) && ((PUSH_NOTIFICATIONS_C2DM_USERNAME && PUSH_NOTIFICATIONS_C2DM_PASSWORD) || PUSH_NOTIFICATIONS_GCM_API_KEY)) { // Determine which method to use for Google push notifications. switch (PUSH_NOTIFICATIONS_GOOGLE_TYPE) { case PUSH_NOTIFICATIONS_GOOGLE_TYPE_C2DM: push_notifications_c2dm_send_message($tokens_android, $payload); break; case PUSH_NOTIFICATIONS_GOOGLE_TYPE_GCM: push_notifications_gcm_send_message($tokens_android, $payload); break; } } }

Friday, 22 July 2016

Drupal 7 changing links of the fly in a Drupal Menu

This is a bit of hack but I thought I’d get it documented here anyway. Please for feel free to message me with better solutions for sorting this issue.

The reason you can’t do this with hook_menu_alter is that this method does not create the menu on the fly; as the menu is built and stored in cache.

Here in my code I’ve got a calculation that returns a NID that I want to replace with the default one that I have in the menu item.

So I’ve added this to the body class list and retrieve it in the Javascript.

/*
 * Implements hook_preprocess_html()

 */

function MODULE_preprocess_html(&$variables){

  global $user;

  drupal_add_js(drupal_get_path('module', 'MODULE') . '/js/MODULE.menu.overrides.js');

// the method here calculates what Nid I need to direct what user too. 
    $getNid = SupplierFormHelpers::getNid($user->uid);

    $variables['classes_array'][] = 'ROLE-logged-in';
    $variables['classes_array'][] = $getNid;

  }

}



and here's the Javascript file

/**
 * @file
 * Javascript to replace NID with the one for the current User
 */
(function ($) {

    /**
     * Process controls.
     */
    Drupal.behaviors.moduleMenuOverrides = {

        attach: function (context) {

            // first of all lets place the number somewhere ( body class !! ) and retrieve that .

            if ($("body").hasClass("ROLE-logged-in")){

                var pageNid = 1;

               /* get the body class array and retrieve the next number along */
                var classList = $("body").attr('class').split(/\s+/);

                for (var i = 0; i < classList.length; i++) {
                    if (classList[i] === 'ROLE-logged-in') {
                        var i2 = i + 1;
                        pageNid = classList[i2];
                    }
                }

                /* i now have the number I want to replace with - so let's do a swap */
                
                var editLink = '/node/' + pageNid + '/edit';
                var navigation = $("#block-system-navigation");
                $("#block-system-navigation").each(function(){
                    var html = $(this).html();
                    html = html.replace("/node/1/edit", roleLink);
                    $(this).html(html);

                })

            }

        }
    }

})(jQuery);

Wednesday, 20 July 2016

Drupal 7 form API trying to use 'commerce_price' type .

I couldn't find a solution to this. Similar to the question raised here.

http://drupal.stackexchange.com/questions/127864/custom-form-adding-a-commerce-price-field

But I adopted another angle of how to display my form which I think is worth listing. As the form I wanted my user to see is pretty much the same as the one I already had in Admin for the entity then I changed the permissions for the front end users Roles to be able to see and edit the form.

I then used Pathauto to change the URL of the path, as the former path had '/admin/structure' at the beginning which I wasn't happy with.

And finally to add and process some other areas of the form that I wanted to was to user 'hook_form_FORM_ID_alter'



So if you searching for things like

drupal 7 Price with currency widget for form

drupal 7 form api example of '#type' => 'commerce_price',

Then maybe you to like me where barking up the wrong tree !!