Skip to main content

PHP

Display node form fieldset using Form State API

Submitted by amitsedai on
Display a group fieldset using form state api. <?php /** * Display group group_name if control field field_control_name has value 2 */ function ji_custom_field_group_build_pre_render_alter(&$element) { if(isset($element['group_name'])){ $element['group_name']['#states'] = array( 'visible' => array( ':input[name="field_control_name[und]"]' => array('value' => 2), ), ); } } ?> -- Links: http://api.drupalhelp.net/api/field_group/field_group.module/function/field_group_build_pre_render/7 http://timonweb.com/programmatically-hiding-a-fieldgroup

Change the text of Apply Button on views Exposed form

Submitted by amitsedai on
The following snippet changes the views exposed form Apply Button to Search Jobs. Make the changes in template.php file of the currently enabled default theme. Clear cache to see it work.(Clear class registry cache to be precise.) <?php function YOUR_THEME_NAME_preprocess_views_exposed_form(&$vars, $hook){ // only alter the required form based on id // drupal_set_message(t("form id: ".$vars['form']['#id']), 'status', FALSE); if ($vars['form']['#id'] == 'views-exposed-form-search-openings-page') { // Change the text on the submit button

How to get the current node object

Submitted by amitsedai on
For Drupal6 , it is recommended to use menu_get_object() <?php $node = menu_get_object(); $story = $node->type == 'story'; ?> However in Drupal 7, node_load() also caches the current node object and in many cases, node_load() returns an object faster than menu_get_object() -- http://www.thecarneyeffect.co.uk/menugetobject-vs-nodeload-when-getting-node-object-drupal-7 https://api.drupal.org/api/drupal/includes%21menu.inc/function/menu_get_object/7

Creating PDF Document using DomPDF

Submitted by amitsedai on
<?php /** * Convert the HTML into a PDF and store it in a particular location. * In this example, the dompdf library is stored under libraries folder in Drupal. */ function ji_custom_convert_pdf($user_html){ //Get path of dompdf folder under libraries set_include_path(libraries_get_path('dompdf')); require_once "dompdf_config.inc.php"; $dompdf = new DOMPDF(); $dompdf->load_html($user_html); $dompdf->render(); $output = $dompdf->output(); file_put_contents("sites/default/files/resume/".$user->name."_resume.pdf", $output); } ?> --

Using Entity Metadata Wrapper

Submitted by amitsedai on
The Entity API provides wrapper classes you may use to make dealing with the values of an entities properties and fields easier. Wrappers make it easier to get and set the values of fields and properties as well as to programmatically retrieve additional information about these elements and iterate over lists of values in a consistent manner. This implies that one need not worry about fetching a node property in the format: $node->field_name['und'][0]['value']

How to enable Ajax for CCK Fields in Drupal 7

Submitted by amitsedai on
The idea is to modify another CCK field based on selection of an item via a select list in another field. Let us consider a content type called article containing 2 additional fields: article_test_entity - which is an entity reference that displays node titles of all nodes and article_test_entity_type - which is a select list that contains Content Type names list with its key as the actual machine name of the content type. The idea is to restrict the list of node titles displayed on article_test_entity based on the content type selected via article_test_entity_type. <?php /*

Rules cron based condition code for allowing execution only once

Submitted by amitsedai on
<?php /* * Part of Rules condition code that returns true if the current time during cron is between 6 am and 7 am */ $ji_daily_status = variable_get("ji_daily_report","1"); //Check if the variable is set, if not set it to 1 [First Time] /* * This fragment checks if the time is between 6 and 7, if so enables the flag * If any other time, sets the ji_daily_report so that next time it executes when cron runs between 6 and 7 */ $time = date('H'); // Get the current time /* Any arbitrary time - Checks if the time is not between 6 am and 7 am */ if($time < 6 || $time > 7){

Displaying Errors in Drupal

Submitted by amitsedai on
During testing phase of the system, there are WSOD for whom no logs are available. Devel is one option, but for knowing just what might be the reason, the following changes in the settings.php file will reveal any error that occured. <?php error_reporting(E_ALL); ini_set('display_errors', TRUE); ini_set('display_startup_errors', TRUE); ?>