0

I have a content type field with machine name field_p_frontip and for that i have a table node__field_p_frontip and in that there is field value like field_p_frontip_value . Eventhough i manually inserted a value in to that field its not taking effect in the front end . I updated the value in the node_revision__field_p_frontip table too and cleared the cache .Why its not working ,any idea ?

I done this because i done via hook_form_alter and its not taking effect .

node__field_p_frontip table structre :

+-----------------------+------------------+------+-----+---------+-------+
| Field                 | Type             | Null | Key | Default | Extra |
+-----------------------+------------------+------+-----+---------+-------+
| bundle                | varchar(128)     | NO   | MUL |         |       |
| deleted               | tinyint(4)       | NO   | PRI | 0       |       |
| entity_id             | int(10) unsigned | NO   | PRI | NULL    |       |
| revision_id           | int(10) unsigned | NO   | MUL | NULL    |       |
| langcode              | varchar(32)      | NO   | PRI |         |       |
| delta                 | int(10) unsigned | NO   | PRI | NULL    |       |
| field_p_frontip_value | longtext         | NO   |     | NULL    |       |
+-----------------------+------------------+------+-----+---------+-------+

and node_revision__field_p_frontip table structure :

+-----------------------+------------------+------+-----+---------+-------+
| Field                 | Type             | Null | Key | Default | Extra |
+-----------------------+------------------+------+-----+---------+-------+
| bundle                | varchar(128)     | NO   | MUL |         |       |
| deleted               | tinyint(4)       | NO   | PRI | 0       |       |
| entity_id             | int(10) unsigned | NO   | PRI | NULL    |       |
| revision_id           | int(10) unsigned | NO   | PRI | NULL    |       |
| langcode              | varchar(32)      | NO   | PRI |         |       |
| delta                 | int(10) unsigned | NO   | PRI | NULL    |       |
| field_p_frontip_value | longtext         | NO   |     | NULL    |       |
+-----------------------+------------------+------+-----+---------+-------+

From Berdir's comment


I was trying to set the value of the field 'field_p_frontip' in form_alter as follows , but both didnt worked .

$form['field_p_frontip']["en"][0]['#default_value']['value'] ='192.168.1.100'; $form['field_p_frontip']['#default_value'] = '192.168.1.100';

  • 1
    Never write into field tables directly. You're asking the wrong question, you should explain what you try to do in your form alter and what code you tried that isn't working. There is definitely a better way. – Berdir Oct 26 '16 at 22:20
  • I am trying to add/modify a field's value in hook_form_alter . Itried like $form['field_p_frontip']["en"][0]['#default_value']['value'] ='192.168.1.100'; $form['field_p_frontip']['#default_value'] = '192.168.1.100' , both didnt worked ,that y i tried modifying db – Crazyrubixfan Oct 27 '16 at 7:18
  • If you want to set the default value of the field, why not simply configure the field. It sounds like you overall are doing something very wrong. If you wrote in your question what you want to achieve, what you have done and where you are stuck, we can help you a lot better. Right now you have only said where you are stuck, but it sounds so wrong that we can't help. Also the code you provided in your update is wrong for Drupal 8. – googletorp Oct 27 '16 at 8:57
  • @googletorp , what i want to do is set the value of a field on hook_form_alter and update the field programmatically , not from front end – Crazyrubixfan Oct 27 '16 at 9:09
  • So what you want to do, is to update the value of a field of one or more existing nodes? – googletorp Oct 27 '16 at 9:29
1

To update the value of a field for a node, you need to.

  1. Load it
  2. Modify it
  3. Save it

In code it looks like this:

$node = \Drupal\node\Entity\Node::load($nid);
if ($node) {
  $node->field_p_frontip = '192.168.1.100';
  $node->save();
}

How you set the value of the field, depends on the field itself. Most fields are simple, and can be set with a scalar value. If field has multiple values or stores more complex data (like the address field), you need to account for this complexity. Usually this is done with arrays.

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.