batch scripting with drupal (featuring the entityfieldquery api)
DESCRIPTION
TRANSCRIPT
![Page 1: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/1.jpg)
Batch Scripting with Drupal(Featuring the EntityFieldQuery API)
Engr. Ranel O. Padon
DrupalCamp Manila 2014
[email protected] | https://github.com/ranelpadon
![Page 2: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/2.jpg)
About Me
Full-time Drupal developer for CNN Travel
Part-time Python lecturer.
Involved in computational Java and Python projects before.
Plays competitive football and futsal.
![Page 3: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/3.jpg)
TOPICS
Why do batch scripting?
How to leverage Entities and the EntityFieldQuery API.
How to implement a batch module?
Sample Actual Use Cases
![Page 4: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/4.jpg)
Why do batching?
There are things that are hard to teach to robots:
spatial awareness and image interpretation.
But there are things that machines could do and easily beat humans:
doing repetitive tasks.
In Drupal, you don't want your site editors to do repetitive tasks:
e.g. updating a field to the same value.
![Page 5: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/5.jpg)
Why do batching?
Avoids PHP Timeout
For implementating intsallation profiles
![Page 6: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/6.jpg)
Why do batching?
Batch processing is execution of a series of programs on a
computer without manual intervention .
Designed to integrate nicely with the Form API , but can also be used
by non-Form API scripts.
![Page 7: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/7.jpg)
Why do batching?
Avoids PHP Timeout (max_execution_time errors)
For long and complex data processing
You can give the admins real-time feedback or summary of results.
![Page 8: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/8.jpg)
When to do batching?
Implementing installation profiles
Used by Drupal's install.php and update.php
Updating the value of a field for all Event nodes.
Deleting all nodes older than 3 years.
Migrating Column nodes to Column taxonomy terms.
Creating custom nodes upon saving a content with an uploaded Excel file,
Batch API is triggered by hook_node_presave() and
goes through each row of the attached Excel file.
![Page 9: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/9.jpg)
The Rise of Entities
“Oh no, I left my stuf f in our house.”
Stuff , just like entities, are useful abstraction.
They could change meaning depending on the context.
![Page 10: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/10.jpg)
The Rise of Entities
In Physics, you could treat each object under study as part icles .
In Drupal, they are called entit ies .
Facilitates a unified way to work
with different data units
Concept simplification contributes
to better modularity, flexibility and maintainability.http://evolvingweb.ca/story/understanding-entity-api-module
![Page 11: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/11.jpg)
The Rise of Entities
Before D7, users and comments didn't have the same power
that nodes (content) had.
no translations, fields, versioning, and so on.
Views (relies on fields) didn’t work with comments and users .
![Page 12: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/12.jpg)
The Rise of Entities
Field is a reusable piece of content. Each field is a primitive data type,
with custom validators and widgets for editing and formatters for display.
Entity Type group together fields (use Entity API for custom ones):
Nodes (content)
Comments
Taxonomy Terms
User Prof i les
![Page 13: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/13.jpg)
The Rise of Entities
Bundles are an implementation of an entity type.
They are subtypes of an entity type.
Bundles (subtypes) like articles, events, blog posts, or products could be
generated from node entity. You could add a f i le download field on
Basic Pages and a subtit le field on Articles.
You could also assign geographic coordinates field to all bundles/entities.
![Page 14: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/14.jpg)
The Rise of Entities
Entity would be one instance of a particular entity type
(specific article or specific user via entity_load()).
Drupal 7 Core provides entity_load(), while the Entity API contrib
module provides entity_save() and entity_delete().
![Page 15: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/15.jpg)
The Rise of Entities
In terms of Object-Oriented Programming:
An entity type is a base class
A bundle is an extended/derived class
A field is a class member, attribute, or property,
An entity is an object or instance of a base or extended class
![Page 16: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/16.jpg)
EntityFieldQuery API
Tool for querying Entities (compared to db_select())
Can query entity properties and f ields
Can query f ield data across entity types:
Fetch all pages and users tagged with taxonomy term “premium”
Returns entity IDs that you could load using entity_load()
Database-agnostic (no issues when migrating from MySQL to PostgreSQL)
![Page 17: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/17.jpg)
EntityFieldQuery API
Fetch all nodes.
![Page 18: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/18.jpg)
EntityFieldQuery API
Fetch all nodes of type “Article”.
![Page 19: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/19.jpg)
EntityFieldQuery API
Fetch all nodes of type “Article”, Published only.
![Page 20: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/20.jpg)
EntityFieldQuery API
Tool for querying Entities
![Page 21: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/21.jpg)
EntityFieldQuery API
Tool for querying Entities
![Page 22: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/22.jpg)
EntityFieldQuery API
Tool for querying Entities
![Page 23: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/23.jpg)
Custom Batch Module
Form API meets Batch API
![Page 24: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/24.jpg)
Custom Batch Module
Batch 12 things, process 5 things at a time
http://www.ent.iastate.edu/it/Batch_and_Queue.pdf
![Page 25: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/25.jpg)
Custom Batch Module
Form Submit trigger
http://www.ent.iastate.edu/it/Batch_and_Queue.pdf
![Page 26: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/26.jpg)
Custom Batch Module
Multiple callback operations and the dynamic $context array (stored in db)
http://www.ent.iastate.edu/it/Batch_and_Queue.pdf
![Page 27: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/27.jpg)
Custom Batch Module
Post Processing
http://www.ent.iastate.edu/it/Batch_and_Queue.pdf
![Page 28: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/28.jpg)
Custom Batch Module
The usual suspects:
mybatch. info
mybatch.module
Then, enable the module in “admin/modules” or using Drush:
$ drush en -y mybatch
![Page 29: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/29.jpg)
Custom Batch Module
Information required in mybatch.module , without a form:
I. URL that will be utilized.
II. Function callback definition for that URL.
Usually contains the setup for the batch process.
III. The batch operation's function definition.
IV. The function definition that will be called after
the batch operation.
Then, enable the module in “admin/modules” or using Drush:
$ drush en -y mybatch
![Page 30: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/30.jpg)
Custom Batch Module
1. The batch module URL (http://localhost/admin/mybatch):
![Page 31: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/31.jpg)
Custom Batch Module
1I. Function callback for the URL, setups the batch process.
![Page 32: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/32.jpg)
Custom Batch Module
III. Define the operation callback.
![Page 33: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/33.jpg)
Custom Batch Module
IV. Define the post-operation callback.
![Page 34: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/34.jpg)
Custom Batch Module
![Page 35: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/35.jpg)
Custom Batch Module
III. Define the operation callback (Enhancement).
![Page 36: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/36.jpg)
III. Define the operation callback (Enhancement).
![Page 37: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/37.jpg)
1I. Function callback for the URL (Enhancement).
![Page 38: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/38.jpg)
Custom Batch Module
Information required in mybatch.module, when using a form:
I. URL that will be utilized.
II. Form callback definition for that URL (hook_form).
III. Form Submit definition.
Usually contains the setup for the batch process.
IV. The batch operation's function definition.
V. The function definition that will be called after the batch operation.
Then, enable the module in “admin/modules” or using Drush:
$ drush en -y mybatch
![Page 39: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/39.jpg)
Custom Batch Module
Batch API with Form API:
![Page 40: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/40.jpg)
Custom Batch Module
I. URL that will be utilized.
![Page 41: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/41.jpg)
Custom Batch Module
II. Form callback definition for that URL (hook_form).
![Page 42: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/42.jpg)
Custom Batch Module
III. Form Submit definition.
![Page 43: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/43.jpg)
Custom Batch Module
Part IV and V don't need to be changed:
![Page 44: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/44.jpg)
Our Use Case 1
We need to update the Short Bio field text format of User Profiles
from Full HTML to Editorial Filter:
![Page 45: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/45.jpg)
Our Use Case 1
Using our last batch module example module (with form), all parts
except part IV and V will be almost similar.
![Page 46: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/46.jpg)
IV (1/4):
![Page 47: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/47.jpg)
IV (2/4):
![Page 48: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/48.jpg)
IV (3/4):
![Page 49: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/49.jpg)
IV (4/4):
![Page 50: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/50.jpg)
V:
![Page 51: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/51.jpg)
Our Use Case 2
Preview map images for City/Country taxonomy terms with no set
value: (http://travel.cnn.com/malaysia)
![Page 52: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/52.jpg)
Our Use Case 2
Preview map images for City/Country taxonomy terms with no set
value: (http://travel.cnn.com/japan)
![Page 53: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/53.jpg)
Our Use Case 2
Batch API with Form API (Updating the preview images,
part I and part V are almost similar to that of Use Case 1)
![Page 54: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/54.jpg)
II. Form callback definition for that URL (hook_form).
![Page 55: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/55.jpg)
III. Form Submit definition.
![Page 56: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/56.jpg)
IV. Batch operations callback (highlights only)
![Page 57: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/57.jpg)
IV. Batch operations callback (highlights only)
![Page 58: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/58.jpg)
IV. Batch operations callback (highlights only)
![Page 59: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/59.jpg)
IV. Batch operations callback (highlights only)
![Page 60: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/60.jpg)
Summary
Information required in mybatch.module , without a form:
I. URL that will be utilized.
II. Function callback definition for that URL.
Usually contains the setup for the batch process.
III. The batch operation's function definition.
IV. The function definition that will be called after
the batch operation.
Then, enable the module in “admin/modules” or using Drush:
$ drush en -y mybatch
![Page 61: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/61.jpg)
Summary
Information required in mybatch.module, when using a form:
I. URL that will be utilized.
II. Form callback definition for that URL (hook_form).
III. Form Submit definition.
Usually contains the setup for the batch process.
IV. The batch operation's function definition.
V. The function definition that will be called after the batch operation.
Then, enable the module in “admin/modules” or using Drush:
$ drush en -y mybatch
![Page 62: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/62.jpg)
Summary
Implementing the Batch API without a form could be also
integrated to Drupal hooks or even Drush.
EntityFieldQuery integration to batch operations callback
will facilitate a more readable, flexible, and maintainable
data fetching in the batch process.
![Page 63: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/63.jpg)
Recommended Links
Batch API Docs:
https://drupal.org/node/180528
Examples Module (Batch API):
http://d7.drupalexamples.info/examples/batch_example
Batch API integration with Drush:
http://www.metaltoad.com/blog/using-drupal-batch-api
EntityFieldQuery Docs:
https://drupal.org/node/1343708
EntityFieldQuery as alternative to Views:
http://treehouseagency.com/blog/tim-cosgrove/2012/02/16/entityfieldquery-let-drupal-do-heavy-lifting-pt-1
![Page 64: Batch Scripting with Drupal (Featuring the EntityFieldQuery API)](https://reader034.vdocuments.net/reader034/viewer/2022042515/54b70ef14a7959a8588b4641/html5/thumbnails/64.jpg)
Long Live Open-Source!