0

Let's say user creates a node of article type.There is a field called my_field which can be added multiple times to that node.If user creates or edits the node leaving my_field blank, a default value something like NA should be created in database and should be visible inside the my_field when user edits node next time.

Tried so far:

Inside page.tpl.php

$query = new EntityFieldQuery();

$query->entityCondition('entity_type', 'node')
      ->entityCondition('bundle', 'article')
      ->propertyCondition('status', 1);

$result = $query->execute();

if (!empty($result['node'])) {
  $nids = array_keys($result['node']);

  foreach ($nids as $nid) {

    $node = node_load($nid);

   if(empty($node->field_my_field))
   {
     $field_language = field_language('node', $node, 'field_my_field');
     $node->field_my_field['LANGUAGE_NONE'][0]['value'] = 'NA';

      field_attach_insert('node', $node);
   }
  }
 }

Note: I don't want to create default value inside field settings as my_field can be added multiple times which can go upto more than 20.

I am using drupal 7.41.

2
  • 2
    You really should not be doing this in page.tpl.php. This code scans all your nodes on every page load, which is a huge performance loss. You should do this in a custom module implementing hook_node_insert() and hook_node_update(). Act only after saving or editing a node. Mar 22 '16 at 12:20
  • I tried the same with above hooks,field value didn't go to databse. Mar 22 '16 at 12:22
1

I suppose it is due to your field_attach_insert(). Usually one should save the entire node using node_save(). However it is a lot easier if you install the entity API module and utilize its Entity Metadata Wrappers.

And I'd really recommend creating a custom module. You will need something similar to this:

function mymodule_node_insert($node) {
  if ($node->type == 'article') {
    mymodule_update_empty_value($node);
  }
}

function mymodule_node_update($node) {
  if ($node->type == 'article') {
    mymodule_update_empty_value($node);
  }
}

function mymodule_update_empty_value($node) {
  $wrapper = entity_metadata_wrapper('node', $node);
  // No value? 
  if  (empty($wrapper->field_myfield->value())) {
    $wrapper->field_myfield->set('NA');
    $wrapper->save();
  }
}
9
  • how abt if it is for specific bundle say article? Mar 22 '16 at 12:28
  • Added the condition in the example, the same applies to status. Mar 22 '16 at 12:29
  • showing error EntityMetadataWrapperException: Invalid data value given. Be sure it matches the required data type and format. in EntityMetadataWrapper->set() (line 122 of /var/www/drupal3.com/sites/all/modules/entity/includes/entity.wrapper.inc). Mar 22 '16 at 12:37
  • is the field a textfield? Mar 22 '16 at 12:39
  • textarea with rows Mar 22 '16 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.