vinx.py
vinx.py

Reputation: 155

Get the value of a Text field in Drupal custom module

I am developing a custom module that allows me to create a custom text field where I need to insert a URL for a HTTP Request.

The only thing I would like to do right now is to get the value from the text field and show somewhere in the node.

Here is the .install code :

function graph_field_enable() {
  $field = array(
    'field_name' => 'graph_field',
    'type' => 'text',
  );
  field_create_field($field);


/**
   * Bind field to a entity bundle.
   */
  $instance = array(
    'field_name' => $field['field_name'],
    'entity_type' => 'node',
    'bundle' => 'station',
  );
  field_create_instance($instance);
}
/**
 * Implements hook_disable().
 *
 * Remove field from node bundle (content type) and then delete the field.
 */

function graph_field_disable() {
  $instance = array(
    'field_name' => 'graph_field',
    'entity_type' => 'node',
    'bundle' => 'station',
  );
  field_delete_instance($instance);
  field_delete_field($instance['field_name']);
  print 'Removed ' . $instance['field_name'] . "\n";
}

The .module file only contains :

<?php

I'm very new to Drupal and i think i hate it.

Upvotes: 0

Views: 2615

Answers (3)

UnsettlingTrend
UnsettlingTrend

Reputation: 462

(Sorry, I want to help flesh out Neha Singhania answer, but don't have enough rep yet to comment on it.)

If you don't already have the associative array that represents the node, load it using the node id with:

$node = node_load($nid);

Once you've got that, you can access any textfield value using either of these, I believe:

$node->field_textfieldname['und'][0]['value'];
$node->field_textfieldname[LANGUAGE_NONE][0]['value'];

The first key in the array specifies what language (undefined/none in those cases), the second key/index says which value of the field (in case you have a multiple value field), and the 3rd is the actual meat and potatoes. For text field, you'll also see:

$node->field_textfieldname[LANGUAGE_NONE][0]['safe_value'];

This is, I believe, created/updated on node_save($nid), and it scrubs the value for any illegal/unsafe values.

It's the same for many field types, but not all. But the method's the same. For instance, if you wanted the value for a field type of entity_reference, it'd look something like this.

$node->field_entityrferencefieldname['und'][0]['target_id'];

Where target_id is an integer/id number of whatever internal entity (node id, user id, etc.) it's referencing. I'd HIGHLY recommend installing the devel module, which gives you the dpm() function; basically, it's a pretty version of print_r, and will display as as Drupal message.

If you stick with Drupal, you're gonna think you hate it at times. A lot. I know. But it's still pretty worth it. (And, from my own experience, seems to make it REALLY easy to find a job...)

Also, this is a question that should probably go on drupal.stackexchange.com

Upvotes: 1

Neha Singhania
Neha Singhania

Reputation: 31

To get the information of any node programmatically, load the node with its node id.
$nid = '1';//This is the first node of your site
$node_info = node_load($nid);
print_r($node_info->field_custom_field[LANGUAGE_NONE][0]['value']);//This will print the value of field_custom_field. You can use any field name here.
And to check this information directly put code in hook_init function for testing, later on you can use where you want.

Upvotes: 1

Michal Przybylowicz
Michal Przybylowicz

Reputation: 1668

Check this:

$node = node_load( $nid );
print_r( $node->graph_field );

Or you can print the entire node:

print_r( $node );

Upvotes: 1

Related Questions