Chapter Three: Presentation: Drupal 8 Module Development

Planet Drupal - Fri, 2015/04/17 - 6:15pm

This session was presented at Bay Area Drupal Camp, San Diego Drupal Camp, Phoenix Drupal Camp, and Stanford Drupal Camp.



Have you written a few simple modules for Drupal 7, and are a little bit nervous to find out the changes you'll be facing in Drupal 8?

Categories:

Aten Design Group: Speeding up Complex Drupal Data Loads with Custom Caches

Planet Drupal - Fri, 2015/04/17 - 5:27pm

Recently we had the task of loading data from a content type with 350 fields. Each node is a University’s enrollment data for one year by major, gender, minority, and a number of other categories. CSV exports of this data obviously became problematic. Even before we got to 350 fields, with the overhead of the Views module we would hit PHP timeouts when exporting all the nodes. If you’re not familiar with Drupal's database structure, each field’s data is stored in a table named ‘field_data_FIELDNAME’. Loading an entire node means JOINing the node table by entity_id with each related field table. When a node only has a handful of fields, those JOINs work fine, but at 350 fields the query runs slow.

On this site we’re also plotting some of the data using highcharts.js. We really hit a wall when trying to generate aggregate data to plot alongside a single university's. This meant loading every node of this content type to calculate the averages, which turned our slow query into a very slow query. We even hit a limit on the number of database JOINs that can be done at one time.

In retrospect this is a perfect case for a custom entity, but we already had thousands of nodes in the existing content type. Migrating them and implementing a custom entity was no longer a good use of time. Instead, we added a custom table that keeps all the single value fields in a serialized string.

The table gets defined with a hook_schema in our module's .install file:

function ncwit_charts_schema() {   $schema['ncwit_charts_inst_data'] = array( 'description' => 'Table for serialized institution data.', 'fields' => array( 'nid' => array( 'type' => 'int', 'default' => 0, 'not null' => TRUE, 'description' => 'node id for this row', ), 'tid' => array( 'type' => 'int', 'default' => 0, 'not null' => TRUE, 'description' => 'intitution term id that this data belongs to', ), 'year' => array( 'type' => 'int', 'default' => 0, 'not null' => TRUE, 'description' => 'school year for this node', ), 'data' => array( 'type' => 'blob', 'not null' => FALSE, 'size' => 'big', 'serialize' => TRUE, 'description' => 'A serialized array of name value pairs that store the field data for a survey data node.', ), ), 'primary key' => array('nid'), );   return $schema; }

The most important part of the array is 'data' with type 'blob', which can be up to 65kB. Not shown is another array to create a table for our aggregate data.

When a new node is saved hook_node_insert() is invoked. hook_node_update() fires both when a new node is saved and when it's updated.

/** * Implements hook_node_insert(). * save serialized field data to inst_data table for a new node * For a new node, have to use this */ function ncwit_charts_node_insert($node) { ncwit_charts_serialize_save($node); }     /** * Implements hook_node_update(). * save serialized field data to inst_data table */ function ncwit_charts_node_update($node) { if (isset($node->nid)) { // we're also calling this function from hook_node_insert // because hook_node_update doesn't have the nid if is a new node ncwit_charts_serialize_save($node); } else { return; } }

Now we actually process the fields to be serialized and store. This section will vary greatly depending on your fields.

function ncwit_charts_serialize_save($node) { // save each value as a simple key => value item foreach ($node as $key => $value) { $data[$key] = $value[LANGUAGE_NONE][0]['value']; }   $fields = array(); $fields['nid'] = $node->nid; $fields['tid'] = $node->field_institution_term[LANGUAGE_NONE][0]['tid']; $fields['year'] = $node->field_school_year[LANGUAGE_NONE][0]['value']; $fields['data'] = serialize($data);   db_merge('ncwit_charts_inst_data') ->key(array( 'nid' => $node->nid, )) ->fields($fields) ->execute();

When a node is deleted we have some clean-up to do.

/** * Implements hook_node_delete(). * Also remove node's data from inst_data */ function ncwit_charts_node_delete($node) { if ($node->type !== 'data_survey') { //only care about data_survey nodes return; }   $query = db_select('ncwit_charts_inst_data', 'i'); $query->fields('i')->condition('i.nid', $node->nid); $result = $query->execute(); $data = $result->fetchAssoc(); if ($data > 0) { db_delete('ncwit_charts_inst_data')->condition('nid', $node->nid)->execute(); } }

When first installed or when fields get changed, we added a batch process that re-saves the serialized strings. Aggregate data is calculated during cron and saved in another table. Rather than loading every node with JOINs, the data comes from a simple query of this custom table.

Pulling the data out of the database and calling unserialize() gives us a simple associative array of the data. To pass this data to highcharts.js we have a callback defined that returns the arrays encoded as JSON. Obviously this gets more complicated when dealing with multiple languages or multi-value fields. But in our case almost everything is a simple integer.

This process of caching our nodes as serialized data changed our loading speed from painfully slow to almost instant. If you run into similar challenges, hopefully this approach will help you too.

Categories:

clemens-tolboom pushed to feature/render-viz at build2be/drupal

On github - Fri, 2015/04/17 - 8:24am
Apr 17, 2015 clemens-tolboom pushed to feature/render-viz at build2be/drupal
  • a9b27b5 Issue #2465425 by jibran, pjonckiere, idebr: Vocabulary listing missi…
  • 3e58aba Issue #2465467 by willzyx, Dom., andypost: SystemInfoController::php(…
  • 154 more commits »

Midwestern Mac, LLC: Thoughts on the Acquia Certified Developer - Front End Specialist Exam

Planet Drupal - Thu, 2015/04/16 - 10:34pm

Previously, I posted my thoughts on the Acquia Certified Developer - Back End Specialist exam as well as my thoughts on the Certified Developer exam. To round out the trifecta of developer-oriented exams, I took the Front End Specialist exam this morning, and am posting some observations for those interested in taking the exam.

My Theming Background

I started my Drupal journey working on design/theme-related work, and the first few Drupal themes I built were in the Drupal 5 days (I inherited some 4.7 sites, but I only really started learning how Drupal's front end worked in Drupal 5+). Luckily for me, a lot of the basics have remained the same (or at least similar) from 5-7.

For the past couple years, though, I have shied away from front end work, only doing as much as I need to keep building out features on sites like Hosted Apache Solr and Server Check.in, and making all my older Drupal sites responsive (and sometimes, mobile-first) to avoid penalization in Google's search rankings... and to build a more usable web :)

Categories:

Achieve Internet Blog: Is Your Site Ready For Google’s New Search Ranking Algorithm?

Planet Drupal - Thu, 2015/04/16 - 8:35pm
How does mobile-friendliness affect Google search rankings? Google reports:
Categories:

Acquia: Drupal is fun to use - meet Karen Grey

Planet Drupal - Thu, 2015/04/16 - 6:49pm
Language Undefined Drupal is more fun - meet Karen Grey

I sat down with Karen Grey at Drupal Camp Brighton 2015 to find out more about who she is and what she does with Drupal. I apologize for taking her out of the code sprints for that time! Since we spoke, Karen has taken on a position as Senior Drupal Developer at i-KOS in their Brighton office.

Categories:

Drupal Watchdog: RESTful Web Services Module Basics

Planet Drupal - Thu, 2015/04/16 - 5:30pm
Article

Drupal 7 does not have built-in support for representational state transfer (REST) functionality. However, the RESTful Web Services module is arguably the most efficient way to provide resource representations for all the entity types, by leveraging Drupal's powerful Entity API. Unmodified, the module makes it possible to output the instances of the core entity types – node, file, and user – in JSON or XML format. Further entity type resources and formats are possible utilizing hooks in added code.

As with any REST solution, the RESTful Web Services module supports all four of the fundamental operations of data manipulation: create, read, update, and delete (CRUD). The corresponding RESTful API HTTP methods are POST, GET, PUT, and DELETE, respectively.

Anyone hoping to learn and make use of this module – especially for the first time – will likely be frustrated by the current project documentation, which is incomplete, uneven, and lacking clear examples. This article – a brief overview – is intended to introduce what is possible with this module, and help anyone getting started with it.

We begin with a clean Drupal 7 installation (using the Standard profile) running on a virtual host with the domain name "drupal_7_test". After installing and enabling the module, we find that it does not have the configuration user interface one might expect. In the demonstration code below, we focus on the node entity type.

Nabbing a Node

The simplest operation – reading an entity instance – is performed using a simple GET request containing the machine name of the entity type and the entity's ID.

Categories:

Drupal Association News: We Love Our Volunteers!

Planet Drupal - Thu, 2015/04/16 - 4:15pm

This week is National Volunteer Week, a week to recognize that volunteerism is a building block to a strong and thriving community.  The Drupal Community is no different: as an open-source project our volunteers are vital to the health and growth of our project.  There are so many roles and levels of contribution within our Drupal ecosystem that we at the Drupal Association wanted to highlight how much your contribution means to us and our work.  I took some time and asked around, here’s some of the glowing praise our staff has to say about our phenomenal volunteers. 

“I am continually impressed with the volunteers that I get to work with.  Not only do they rock at their jobs, but they are so dedicated to the work that they do for Drupal and the Cons specifically!  Anyone who has volunteered for a Con knows that it is a large undertaking, and a responsibility that isn't taken lightly. These volunteers come back each week with positive attitudes, valuable ideas and great results.  Although I have only been at the Association for a little over six months, I can truly say that these volunteers are what gives our Cons the 'special sauce' and I am lucky to get to work with volunteers from around the globe on a daily basis.” 

- Amanda Gosner, DrupalCon Coordinator

“Most of my day is spent with Drupal Association staff, who have the luxury of getting paid to think about Drupal for 8 hours a day. A good chunk of my job is working with volunteers though-- the Board of Directors, Drupal.org Working Groups, Community Organizers, DrupalCon session speakers. So many of you give so much of your time and your smarts back to the project and the community, and it's my privilege and duty to learn from you all.”

- Holly Ross, Executive Director

"I look forward to working working with community volunteers to help build and improve Drupal.org. The site would not be where it is today without everyone's work."

- Neil Drumm, Drupal.org Lead Architect

 

“I want to thank Cathy and Jared for being my sprint mentor at DrupalCon Latin America. I made my first comment on the issue queue. It felt so good to cross into that world finally, even if it is was just a baby toe crossing over.”

- Megan Sanicki, COO

 

“It feels like I’m hearing news every day about the amazing programs our community members put together all over the world — from Los Angeles to Uganda and beyond. Without help from amazing community volunteers who donate time working on social media, in the issue queues, or even volunteers who take a brief moment to drop a note in my inbox (“have you seen this?”), these stories would never be shared with our wider community.” 

- Leigh Carver, Content Writer

Today, we invite you to take a few minutes to recognize your fellow Drupal contributors by tweeting or sending a message via IRC to appreciate each other.  After all, without our volunteers, our Drupal Community would not be as lively, bright, and welcoming.  Want to lend a hand?  Our get involved page has plenty of ways to volunteer with the project.

Categories:

KnackForge: Drupal 7 - Hooking Ajax events and views refresh

Planet Drupal - Thu, 2015/04/16 - 3:40pm
Drupal has a solid Ajax interface, we can hook into the Ajax events at various places. I will explain some 5 important methods,   1) beforeSerialize - called before data is packed and runs before the beforeSend & beforeSubmit 2) beforeSubmit - called before the ajax request 3) beforeSend - called just before the ajax request 4) success - called after ajax event returns data 5) complete - called after the request ends   Lets say you want to capture some ajax event (in built or made by other module) to do some event like Views refresh. We can use a very simple logic to do that.  
Categories:

Pronovix: Prototyping LinkForward with Drupal, a startup case study

Planet Drupal - Thu, 2015/04/16 - 3:10pm

This is the story of how I built a first prototype for LinkForward, a web application for power networkers, and how I built it in record time using Drupal (12 hours), without a single line of custom code.

Categories:

Cruiskeen Consulting: Drupal for Developers Second Edition

Planet Drupal - Wed, 2015/04/15 - 11:53pm

Almost everyone who does any form of Drupal development uses Drush - it's the Swiss Army Knife of the Drupal world. Drush is the Drupal Shell, and it lets you do a whole lot of amazing things with Drupal sites without actually going to the site, logging in, and clicking buttons.  It's a command-line tool (and since I'm an old UNIX hand, it's just right for me.

Categories:

Drupal.org frontpage posts for the Drupal planet: A new way to welcome newcomers on Drupal.org

Planet Drupal - Wed, 2015/04/15 - 10:04pm

The first initiative on the Drupal.org 2015 roadmap is ‘Better account creation and login’. One of the listed goals for that initiative is “Build a user engagement path which will guide users from fresh empty accounts to active contributors, identifying and preventing spammers from moving further.” This is something Drupal Association team has been focusing on in the last few weeks.

The first change we rolled out a few days ago was a ‘new’ indicator on comments from users whose Drupal.org accounts are fewer than 90 days old. The indicator is displayed on their profile page as well. We hope this will help make conversations in the issue queues and forum comments more welcoming, as people will be able to easily see that someone is new, and probably doesn’t know yet a lot about the way community works.

Today we are taking another step towards making Drupal.org more welcoming environment for new users. But first, a bit of background.

New users and spam

It is not a surprise for anyone that a big number of user accounts registering on the site are spam accounts. To fight that and prevent spam content from appearing on Drupal.org, we have a number of different tools in place. Of course, we don’t want these tools to affect all active, honest users of the site, and make their daily experience more difficult. To separate users we are sure about from those we aren’t sure about yet, we have a special ‘confirmed’ user role.

All new users start without such a role. Their content submissions are checked by Honeypot and Mollom, their profiles are not visible to anonymous visitors of the site, and the types of content they may create are limited. Once a user receives a ‘confirmed’ role, his or her submissions will not be checked by spam fighting tools anymore; their profile page will be visible to everyone, and they will be able to create more different types of content on the site.

This system works pretty well, and our main goal is to ensure that valid new users get the ‘confirmed’ role as quickly as possible, to improve their experience and enable them to fully participate on the site.

The best way to identify someone as not a spammer is have another human look at the content they post and confirm they are not spammers. Previously, we had a very limited number of people who could do that-- about 50. Because of that, it usually took quite some time for new user to get the role. This was especially noticeable during sprints.

Today we’d like to open a process of granting a ‘confirmed’ role to the thousands of active users on the site.

‘Community’ user role

Today, we are introducing a new ‘Community’ role on the site. It will be granted automatically to users who have been around for some time and reached a certain level of participation on Drupal.org. Users who have this role will be able to ‘confirm’ new users on the site. They will see a small button on comments and user profile of any user who has not yet been confirmed. If you are one of the users with ‘Community’ role, look out for this new Confirm button, and when you see one next to a user - take another look at what the person posted. If their content looks valid, just click ‘confirm’. By doing so, you will empower new users to fully participate on Drupal.org and improve their daily experience on the site.

With expect to have at least 10,000 active users with the ‘Community’ role. With so many people to grant the ‘confirmed’ role, new users should be confirmed faster than ever before.

If you aren’t sure if you have the ‘community’ role or not, don’t worry. We will send an email notification to every user whose account receives the new role. The email will have all the information about the role and how to use it.

Thanks for helping us make Drupal.org a better place!

Front page news: Planet Drupal
Categories:

Drupal Watchdog: VIDEO: DrupalCon Amsterdam Interview: MortenDK

Planet Drupal - Wed, 2015/04/15 - 7:39pm

On a sunny Amsterdam morning, we catch Morten (Tag1 Consulting, Geek Röyale) as he speeds through DrupalCon’s RAI Convention Center on an urgent Viking mission. We waylay him long enough for this brief, Danish-accented, rapid-fire chit-chat.

MORTEN: I am Morten, also known as MortenDK.

RONNIE RAY: You gave a talk this morning?

MORTEN: Yes, I gave a talk about the Drupal 8 Twig project, which is the new theming layer for Drupal. And I gave a demo on all the new and exciting things we have in it.

So that was really good to show off from a front-ender’s perspective everything that was done over the last couple of years and how the new system is going to work in Drupal 8. It was a real gas to finally really show it off. People could see we’re not just lying, but it’s actually real.

RR: So, can I ask you, what are you reading now?

MORTEN: What?

RR: Any books, any magazines?

MORTEN: Ah – oh – uh – (bleep) – what’s the name of it? I actually have it on an audio file, it’s a fantasy story about... uh.. a lot of blood, a lot of personal vendettas. Good clean fun. But actually I haven’t had the time to read for a long time because I’ve been doing so much work on the Drupal project and I’ve been moving. Also, I took up my old hobby of painting miniatures again, just to geek out.

I’m a metal-head so pretty much anything... been into a lot of Opeth, a Swedish metal band – kind of a grown man’s metal. (Indecipherable.)

RR: Do you follow anyone on Twitter or FaceBook?

MORTEN: A couple, but normally not. Interviews with musicians are not always the most interesting thing, it’s the same thing as interviews with sports people, “So how did it go today?” “We played really hard!” “On the new album, we’re going to really show off.” So that’s kind of like... a couple of people... there’s a Swedish band called Arch Enemy I’ve been following closely.

RR: What’s the most exciting thing about Drupal 8 for you?

MORTEN: It is the front-end, the Twig system and the templates, and the way we have shifted focus in the front-end completely around, from being an afterthought to a whole new system that is built for front-enders instead of built by back-enders to front-enders. It’s kind of, we’ve taken control over our own destiny, and that I think is going to be the killer app for Drupal 8.

Tags:  DrupalCon DrupalCon Amsterdam Video Video: 
Categories:

Promet Source: How Drupalers Can Get Organized: Tips From a Librarian

Planet Drupal - Wed, 2015/04/15 - 7:26pm

Ahh, libraries. The smell of books, the sounds of pages turning, the incessant Karaoke singalongs of the library workers. OK, maybe the last one is a bit far-fetched, but we all know it’s founded in some truth. The fact remains that libraries are a hallowed ground of information consumption and organization.

That organization doesn’t happen by dint of chance. No, there’s a lot of hard work that goes into maintaining a collection, and these steps taken by your local library workers might inspire us to approach our websites with the same set of diligent hands. Get sorting, people!

Categories:

Wellnet Blog: Weekly Module Review - #7 Fast Permissions Administration, insert permissions without issues!

Planet Drupal - Wed, 2015/04/15 - 3:03pm

This week we’ll talk about FPA (Fast Permissions Administration), a very cute module I only discovered recently.

Categories:

Code Karate: Installing and configuring a Drupal 7 Sub-theme - 3 of 3

Planet Drupal - Wed, 2015/04/15 - 1:44pm
Episode Number: 202

In the final video of the 3 part series, we look at creating and configuring a Drupal sub-theme. Specifically, we will be created a sub-theme based off of the Zen theme. If you aren’t familiar with Zen, it is a very popular base theme used by thousands of designers as a starting point when building a custom website theme.

Tags: DrupalDrupal 7Theme DevelopmentDrupal Planet
Categories:

InternetDevels: Drupal for dummies: where and when is Drupal the Best Option?

Planet Drupal - Wed, 2015/04/15 - 12:21pm

Let us give the floor to Jack Dawson, founder of Big Drop Inc. He shared his thoughts and ideas about Drupal with the readers of our blog.

Drupal is an open source CMS (content management system), which means that its block of code is available for extension and modification by anyone with programming knowledge. This is as opposed to close source/proprietary software, whose creators retain its IP rights. Open source software does not come with any fees, and it is fully modifiable to fit any user requirements.

Read more
Categories:

Free Energy Media: Simple REST API SMS App Integration with Telecom Provider

Planet Drupal - Wed, 2015/04/15 - 10:34am

I have been building apps recently that integrate a REST API which subscribes users to a Drupal web app via SMS, they are simultaneously subscribed in the telecom operators database. Conditions must be checked to keep the users status in sync with Drupal and the operator. Other conditions that I won’t cover here include recurring billing or the free trial period.

This is a practical example of how using the REST protocol allows technologies that are completely different to communicate with each other. The technologies in our stack include SMS, mobile billing and a Drupal web app.

First create the URL endpoint by using hook_menu

function my_module_menu() {
$items['my_url/send'] = array(
'title' => 'send',
'page callback' => 'my_module_send_page',
'access callback' => TRUE
);
return $items;
}

When a mobile originated text message “MO” is sent from the customer handset to the shortcode created for our app, a relay message is sent from the telecom integrator’s API and hits the endpoint created with hook_menu on our web app. We then use a GET request to get the values from the values posted to our endpoint url. Parameters include product identification information, partner ID, customer MSISDN and subscription method, as well as other info. We also run an additional API call to check if the MSISDN/phone number is valid and if there is enough money on the customer account.

Here we have the beginning of the callback function that runs when our endpoint is hit.

function my_module_send_page() {

if (isset($_GET['Origin'])) {
$mobile = $_GET['Origin'];
$PricePointId = $_GET['PricePointId'];
$prodId = $_GET['ProductId'];
$mtmo = time() . 'MO';

There are many conditions in the business requirements, which determine different messages in the $text variable to the customer. Success/failure, weekly/monthly, arabic/english, below is the case if success and weekly PricePointId is selected. There is also a check for what language is selected, here we use the language value in the user object, we get this from what language the user selects on the registration form. We then pass back the correct language in the $text variable in a post request using CURL with everything else. Here is some more of the callback function.

if ($PricePointId == XXXXXX) {
if($user->language == 'en') {
$text = 'You have successfully subscribed
}
else {
$text = 'تمّ إشتراكك بملهمتي بنجاح بسعر ‘
}
}

$data2 = array(
'Password' => 'test',
'ProductId' => $prodId,
'PricePointId' => $PricePointId,
'SenderId' => 92235,
'OpId' => XXX,
'Destination' => $mobile,
'Text' => $text,
'ExtTxId' => $mtmo,
);

$qry_str = drupal_http_build_query($data2);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, 'http://integrator-api-endpoint/sendMT?' . (string) $qry_str);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);

Here we get the result from the CURL post to the telecom API.
If the result is greater than one that is success, so we create the user in Drupal and we send a custom email using drupal_mail().

if($result > 0) {
$obj->field_product = $prodId;
$obj->save(); */
$smart_db->changeUserRoles($user->uid , 'subscribe');
$smart_db->changeplan($user->uid, $prodId, $free = TRUE);
$to=$user->mail;
drupal_mail('user', 'register_no_approval_required', $to, user_preferred_language($user, $default = NULL), array('account' => $user), variable_get('site_mail', ''));
}
}

Categories: