13th Sep, Drupal 7 advanced training by TCS

Post on 13-Dec-2014

582 views 1 download

description

13th Sep, Drupal 7 advanced training by TCS

Transcript of 13th Sep, Drupal 7 advanced training by TCS

Drupal Mumbai

In Association With

TATA Consultancy Services

Drupal Global Tranining Day

Saturday Sep 13, 2014

DrupalMumbai.org &

TCS

Drupal Advanced Overview ●  What Is Drupal? ●  Drupal Technology Stack ●  Drupal Core Overview ●  Drupal Bootstrap Phases

Drupal 7 Module Development ●  Types of Modules ●  Creating .info .module and .inc files ●  Using Drupal hooks ●  Using Drupal Forms (FAPI) ●  Using Drupal Schema ●  Using Drupal Variables ●  Using Drupal Blocks ●  Using Drupal Database API ●  Using Drupal Entity API

Types of Modules ●  Types of Modules : Core , Contrib, Custom ●  Core modules that ship with Drupal and are

approved by the core developers and the community

●  Contributed modules written by the Drupal community and shared under the same GNU Public License (GPL) as Drupal.

●  Custom modules created by the developer – often for a particular use case specific to the site they're working on.

Creating .info .module and .inc files

●  Creating .info .module and .inc files ●  .info: Drupal uses .info files to store metadata

about themes and modules and this file is required for the system to recognize the presence of a module.

●  Required: name, description, core, ●  Optional: stylesheets, scripts, files, dependencies,

package, php, version, configure, required, hidden ●  .module : where all Drupal API and module

specific hooks resides ●  .inc : where configuration resides

Hooks •  What is hook? •  Drupal's module system is based on the concept of "hooks". A hook is a

PHP function that is named foo_bar(), where "foo" is the name of the module (whose filename is thus foo.module) and "bar" is the name of the hook. Each hook has a defined set of parameters and a specified result type.

•  How hooks work •  A module need simply implement a hook. When Drupal wishes to allow

intervention from modules, it determines which modules implement a hook and calls that hook in all enabled modules that implement it.

Heart of Drupal

menu dispatching process

hook_menu •  API Url :

https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_menu/7.

•  Hook Used: This hook enables modules to register paths in

order to define how URL requests are handled with access control

•  Hook Invocation: On module Enable and cache clear.

Menu Example /** * Implementation of hook_menu(). */ function menufun_menu() { $items['menufun'] = array(

‘title’ => ‘Greeting’, 'page callback' => 'menufun_hello', 'access callback' => TRUE, 'type' => MENU_CALLBACK,

); return $items; }

Properties (commonly used) :

•  title : A required field that represents the untranslated title of the menu item.

•  description: The untranslated description of the menu item.

•  page callback : PHP function to call when user visits the path.

•  page arguments : An array of arguments to pass to the page callback function.

•  access callback : Function returning a Boolean value that determines whether the user has access rights to this menu item.

•  access arguments: An array of arguments to pass to the access callback function.

•  file: A file that will be included before the callbacks are accessed; this allows callback functions to be in separate files.

•  weight: An integer that determines the relative position of items in the menu.

•  type: A flag describing properties of the menu item.

•  MENU_NORMAL_ITEM: Normal menu items show up in the

menu tree and can be moved/hidden by the administrator. •  MENU_CALLBACK: Callbacks simply register a path so that

the correct function is fired when the URL is accessed. •  MENU_SUGGESTED_ITEM: Modules may “suggest” menu

items that the administrator may enable. •  MENU_LOCAL_TASK: Local tasks are rendered as tabs by

default. •  MENU_DEFAULT_LOCAL_TASK: Every set of local tasks

should provide one “default” task, which links to the same path as its parent when clicked

hook_permission API Url :

https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_permission/7

Hook Used : Supply module define permissions,

can be selected from permission page to give or restrict access

Hook Invocation : On visiting admin/people and

admin/people/permissions only not on cache clear

Hook_permission Example /** * Implementation of hook_permission (). */ function menufun_permission () { return array( 'administer my module' => array( 'title' => t('Administer my module'), 'description' => t('Perform administration tasks for my

module.'), ), ); } Api : user_access('administer my module’);

Properties :

•  title : The human-readable name of the permission •  description : A description of what the permission does •  restrict access : A boolean which can be set to TRUE to

indicate that site administrators should restrict access to this permission to trusted users

•  warning : A translated warning message to display for this permission if restrict access true

Using Drupal Forms (FAPI) ●  Using Drupal Forms (FAPI) : Used to create forms ●  Forms can be used to store configuration into

drupal system variables ●  hook_form() , hook_form_validate(),

hook_form_submit() ●  Reference: ●  https://api.drupal.org/api/drupal/developer!topics!

forms_api_reference.html/7 ●  https://www.drupal.org/node/650016 ●  https://api.drupal.org/api/drupal/includes!form.inc/

group/form_api/7

Drupal FAPI ●  API Url :

https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7

●  What is Form api in drupal7 : The Form API is

nothing but different types of methods,attribute and properties to collect data from user in a very secure way .

How Form Invocation works

How Form Invocation works ●  Initializing the Process : ●  $form_id : a string identifying the form. ●  $form : is a structured array describing the form. ●  $form_state : contains information about the form,

such as the form’s values and what should happen ●  when form processing is finished.

drupal_get_form() begins by initializing $form_state.

Form Elements # Text Field # Textarea # Select # Radio Buttons # Check Boxes # Date # File Upload : $form['picture']['picture_upload'] = array(

'#type' => 'file',

'#title' => t('Upload picture'),

'#size' => 48,

'#description' => t('Your virtual face or picture.')

);

# Fieldset : group elements together $form['author'] = array(

'#type' => 'fieldset',

'#access' => user_access('administer nodes'),

'#title' => t('Authoring information'),

'#collapsible' => TRUE,

'#collapsed' => TRUE,

'#weight' => 20,

);

# Textarea : $form['keywords'] = array( '#title' => t('Keywords'),

'#type' => 'textarea',

'#description' => t

('The comment will be unpublished if it contains any of the phrases above. Use a case-sensitive, comma-separated list of phrases. Example: funny, bungee jumping, "Company, Inc."'),

'#default_value' => isset( $context['keywords']) ? drupal_implode_tags($context['keywords']) : '',

);

# Radio : $active = array(0 => t('Closed'), 1 => t('Active'));...

$form['settings']['active'] = array(

'#type' => 'radios',

'#title' => t('Poll status'),

'#default_value' => isset($node->active) ? $node->active : 1,

'#options' => $active,

'#description' => t('When a poll is closed, visitors can no longer vote for it.'),

'#access' => $admin,

);

<form_id>_submit: This is a hook used by node modules. It is called after validation has succeeded and before insert/update. function hook_submit(&$node) {

// if a file was uploaded, move it to the files directory

if ($file = file_check_upload('file')) {

$node->file = file_save_upload($file, file_directory_path(), false);

}

}

#hook_validate: Perform node validation before a node is created or updated function hook_validate($node, $form, &$form_state) {

if (isset($node->end) && isset($node->start)) {

if ($node->start > $node->end) {

form_set_error('time', t('An event may not end before it starts.'));

}

}

}

hook_form API Url : https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7 What is Form api in drupal7 : The Form API is nothing but different types of methods,attribute and properties to collect data from user in a very secure way .

Using Drupal Schema ●  Data Typs: https://www.drupal.org/node/159605 ●  Schema : A Drupal schema definition is an array

structure representing one or more tables and their related keys and indexes.

●  A schema is defined by hook_schema(), which must live in the modulename.install file.

●  https://www.drupal.org/node/146939 ●  Using hook_update_N() to run dtabase updates ●  Schema API functions

https://www.drupal.org/node/150223

Using Drupal Schema ●  Primary key fields must be NOT NULL ●  Adding NOT NULL columns with no default ●  Overview of NULL, NOT NULL, and defaults ●  Match column and default value type ●  INSERT statements for text columns ●  Updating tables: don't use hook_schema()

Drupal 7 Variable API ●  Using Drupal Variables system ●  Variable_get() ●  Variable_set() ●  Variable API: modulename.variables.inc

Drupal 7 Using Blocks ●  Drupal Core Blocks ●  Drupal Custom Blocks ●  Writing hook_block_info() ●  Writing hook_block_view() ●  Adding template file to Block

Drupal 7 Database API ●  The Drupal 7 Database API provides a standard,

vendor-agnostic abstraction layer for accessing database servers.

●  Drupal DB API allows to support multiple database servers easily; to allow developers to leverage more complex functionality, such as transactions; to provide a structured interface for the dynamic construction of queries; to enforce security checks and other good practices; to provide modules with a clean interface for intercepting and modifying a site's queries

Drupal 7 Database API ●  Refrence: https://www.drupal.org/developing/api/database https://www.drupal.org/files/

er_db_schema_drupal_7.png

Drupal 7 Entity API ●  Entity types: An entity type is a useful abstraction

to group together fields. Eg: Nodes (content), Comments, Taxonomy terms, User profiles. It uses hook_entity_info() and Entity API Module

●  Bundles: Bundles are an implementation of an entity type to which fields can be attached. You can consider bundles as subtypes of an entity type. Eg: With content nodes (an entity type), for example, you can generate bundles (subtypes) like articles, blog posts, or products.

●  Not all entity types have bundles

Drupal 7 Entity API ●  Fields: A field is a reusable piece of content which

can be attached to Entity using Field API of Drupal. Fields relates to Entities is that Fields can be added to any of the bundles (or entity types) to help organize their data.

●  Entity: An entity would be one instance of a particular entity type such as a comment, taxonomy term or user profile or a bundle such as a blog post, article or product.

●  Entity API module helps to provide functions like entity_create(),entity_save(),entity_delete(), entity_view(), entity_access()

Drupal 7 Entity API ●  An entity type is a base class ●  A bundle is an extended class ●  A field is a class member, property, variable or

field instance (depending on your naming preference)

●  An entity is an object or instance of a base or extended class

●  All these four OOD/P concepts are special in that they are serialisable (stored - e.g. to a database or file). Serialisation takes place via the Entity API.

Drupal 7 Entity API ●  Providing New Entity Type: Entity CRUD API

allows you to easily create a new entity type ●  Depend on the entity module. ●  Describe your entities db table as usual in

hook_schema(). Add any columns specific to your entity. For some examples, see node_schema() and user_schema().

●  Implement hook_entity_info() for your entity. At a minimum, specify the controller class of this API, your db table and your object's primary key field. Optionally also set the 'entity class' to Entity, or your extended class.

Drupal 7 Entity API ●  Advantages of Entity API:

−  Making Entity exportable −  Making Entity revisionable −  Leavrage Admin UI for Add / Edit / Delete −  Makes Entity Type / Bundles / Entity / Custom

Table and Fields of Entity avalible for Views ●  Viewing Entity : entity API assists in writing the

code for viewing an entity by providing entity_build_content() and entity_view() functions which take care of attaching any fields and works simliar to the the node_view() function.

Drupal 7 Entity API ●  Basic file skeleton + CRUD hooks

https://www.drupal.org/node/999938 ●  View-related hooks

https://www.drupal.org/node/999954 ●  Views Integration

https://www.drupal.org/node/1208874

Thank You

Dinesh W Harshil M Vijay M

Rachit G Nidhi B

Harish N Amol T

Sidhartha P 31st May 2014

Drupal Global Training Day, Mumbai, TCS

Thank You!

Website: http://drupalmumbai.org Email: info@DrupalMumbai.org Twitter: @DrupalMumbai Facebook: http://fb.me/DrupalMumbai Google+: http://gplus.to/DrupalMumbai