Skip to main content

Drupal API

Update Drupal 7 text fields max length for fields having data

Submitted by system on
<?php
/*
* Utility to change the max length of a text field
*/
function change_text_field_max_length($field_name, $new_length) {
$field_table = 'field_data_' . $field_name;
$field_revision_table = 'field_revision_' . $field_name;
$field_column = $field_name . '_value';

// Alter value field length in fields table
db_query("ALTER TABLE `{$field_table}` CHANGE `{$field_column}` `{$field_column}` VARCHAR( {$new_length} )");
// Alter value field length in fields revision table

API changes in Drupal 8 in comparison to Drupal 7

Submitted by system on
This post would reflect on few essential APIs in Drupal 7 and how they were changed in Drupal 8: drupal_get_title()
: Gets the title of the current page. Link to changelog here . Drupal 7: <?php $title = drupal_get_title(); ?> Drupal 8: <?php $request = \Drupal::request(); $route_match = \Drupal::routeMatch(); $title = \Drupal::service('title_resolver')->getTitle($request, $route_match->getRouteObject()); ?> -- Links: https://www.drupal.org/node/2067859

Displaying field values with formatting

Submitted by system on
Displaying data for any entity fields with formatting can be achieved with 2 simple drupal api. field_get_items: Returns the field items in the language they currently would be displayed. field_view_value: Returns a renderable array for a single field value. Example: <?php $status_field = field_get_items('node', $node, 'field_status'); print render(field_view_value('node', $node, 'field_status', $status_field[0])); ?> -- Source https://api.drupal.org/api/drupal/modules%21field%21field.module/function/field_get_items/7 https://api.drupal.org/api/drupal

Drupal l() and url() methods

Submitted by amitsedai on
Both l and url methods are handy to create urls for various entities. However, the variety of options available makes the usage exciting and complex. l(): Formats an internal or external URL link as an HTML anchor tag. Syntax: l($text, $path, array $options = array()) Creating an absolute URL using l(). The example below creates a node link. <?php l(t('New product'), 'node/123', array('absolute' => TRUE)); ?> Add the previous url or the current page url before going to the new page.

Alter field title of content type to display description below the field title

Submitted by system on
This can be achieved using hook_node_view <?php function ji_custom_node_view($node, $view_mode, $langcode) { if($view_mode != 'full'){ return; } switch ($node->type) { case 'blood_donor_form': foreach ($node->content as $field_name => $field_options) { if(isset($field_options['#title']) && isset($field_options[0]['markup'])) { $node->content["$field_name"]['#title'] = $field_options['#title'] ."".$field_options[0]['mark

Get list options of a field

Submitted by system on
We may want to get the options available for a field in content type. This can be achieved using Field API. <?php $blood_group_field = field_info_field("field_blood_group"); $blood_groups = list_allowed_values($blood_group_field); ?> where "field_blood_group" is the field machine name.

Drupal File Operation

Submitted by system on
This post will illustrate various handy file operations available in Drupal Get Absolute Path of a file from URI : drupal_realpath() A URI in drupal looks like: private://donor_registration/somefile.csv for a private file Absolute Path: /srv/www/example.com/public_html/sites/default/private/files/donor_registration/somefile.csv <?php $absolute_path = drupal_realpath($uri); drupal_set_message(t("Realpath is: $absolute_path"), 'status', FALSE); ?> -- Sources: https://api.drupal.org/api/drupal/includes!file.inc/function/drupal_realpath/7

Creating exportable field definitions in Drupal 7

Submitted by amitsedai on
There are times when we would want to create similar data structures across multiple drupal sites for common functionalities. Although this can be done using features but when the effort involves one time exercise and involves just few fields to be exported, using custom code for creating export definitions for importing in the other site can be a simpler alternative. If you want export a field definition, you can use the handy field_info_field and field_info_instance functions.