0

and thank you for your patience in helping me learn

I have created a content type called response (Through Drupal, not in a custom module)

In the content type I have a field called field_response_number which is a text field.

When I create new content for the response content type and press submit I want to first submit the form, then run a custom submit afterwards that populates my field_response_number based on the result of a custom function.

I can submit and call my custom form submit function fine, but how do I get the total returned from my custom function to populate the field_response_number please

So my work flow is

  • Create new response content
  • Submit results in form and then call custom submit handler
  • Get count from DB
  • Populate the field_response_number with the result
  • Display result

Can anyone assist please, as I think I'm almost there

My code is as follows

function testmodule_form_node_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  foreach (array_keys($form['actions']) as $action) {
    if ( $form_id == 'node_response_form'){
      if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
        $form['actions'][$action]['#submit'][] = 'testmodule_form_submit';
      }
    }
  }
}

function testmodule_form_submit(array $form, FormStateInterface $form_state){
  // This works
  //$result = testmodule_get_node_count('response');
  //drupal_set_message($result);

  How do I populate my field_response_number here please
}

//This handles incrementing by one I want to use to populate the field_response_number with
function testmodule_get_node_count($content_type) {
  $query = "SELECT COUNT(*) FROM {node} n ".
           "WHERE n.type = :type";
  $total = db_query($query, array(':type' => $content_type))->fetchField();
  return $total;
}

Many Thanks for your assistance

  • Can't you just use the nid (node ID) as your reference? This automatically gets incremented with each node created. As with your logic if a response node gets deleted you'll end up with duplicate counts. e.g. you have responses, 1,2,3,4 and then you delete response 2. The next one created won't be 5 it would be 4 again. Atleast the nid would be something unique. – Leigh May 16 at 9:12
  • Many Thanks, I intend to add a second argument that checks another field value and adds that to the count, so node ID won't work, and they will not be able to delete but not got that far yet. My question was more how I can solve the issue of populating my field_response_number through the custom submit, even if just with text, that's what Im currently struggling with...thanks – user71404 May 16 at 9:17
  • Drupal 7 or Drupal 8? (I guess 8) – golddragon007 May 16 at 9:18
  • Sorry, this relates to Drupal 8 not D7 – user71404 May 16 at 9:26
0

I guess you have multiple options.

  1. You set the field value in the alter as a #value, this is not editable by the user, not like #default_value. Then drupal will save it automatically on successful save.
  2. You set the field value in the validate callback into the $form_state variable. Not too elegant, but the drupal will save the value automatically (normally) on successful submit.
  3. You create a custom submit (as you did) and then you load your node (D7: node_load($nid) D8: \Drupal::entityTypeManager()->getStorage('node')->load($id);), set the field value (D7: $node->field_name[LANGUAGE_NONE][0]['value'] = 'something'; D8: $node->set('field', 'field_value');), and then save the node (D7: node_save($node);, D8: $node->save();).
  4. You can use hook_ENTITY_TYPE_insert() to update your field and save the entity again. (https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21entity.api.php/function/hook_ENTITY_TYPE_insert/8.2.x)
  5. You can use hook_ENTITY_TYPE_presave() you just set the field value here, you don't need to save it. Drupal's next step will the save. (https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21entity.api.php/function/hook_ENTITY_TYPE_presave/8.2.x)

For D8 a bit more detailed: https://www.heididev.com/how-programmatically-update-fields-value-drupal-8
And also, maybe the entity is already loaded, check this: https://www.drupaleasy.com/quicktips/drupal-8-getting-entity-id-formstate

  • Thats fantastic thanks, Is there a version of 3 for D8? – user71404 May 16 at 9:30
  • @gibbo, Yes, updated, please check the links too – golddragon007 May 16 at 9:38
  • Tried reading those but didn't really understand fully, how would I pass my node nid to my custom submit function then? Thanks – user71404 May 16 at 10:13
  • you need to debug the $form_state I think that already contains the 'node' and the nid too. – golddragon007 May 16 at 10:48
0

You just need the following

       function testmodule_form_submit(array $form, FormStateInterface $form_state){

              $nid = $form_state->getformObject()->getEntity()->id();

              $node = \Drupal::entityTypeManager()->getStorage('node')->load($nid);
              $node->set('field_response_number', 'field_value');
              $node->save();
        }
  • If you have already the Entity, why you reload it? – golddragon007 May 16 at 11:06
  • I found just doing $node = \Drupal::entityTypeManager()->getStorage('node')->load($id); generates an error with this approach, so reloaded it and all works fine. Just my quick test though – Pete May 16 at 11:11
  • Of course it generates error, because $id is null and load() waits an integer. I speaking about if you have the loaded entity by $form_state->getformObject()->getEntity() why you reload it, instead of using $node = $form_state->getformObject()->getEntity()? (class check and typecheck may required) – golddragon007 May 16 at 11:54
  • Probably my lack of knowledge but '$node = $form_state->getformObject()->getEntity(); $node->set('field_response_number', 'field_value'); $node->save();' generates errors. Sounds like there is a better way, and always keen to learn, but that's how I have done it till now. – Pete May 16 at 12:42
  • You cannot use this code in the form submission handler for the node edit form, when a new node is being created. – kiamlaluno May 16 at 16:00

Your Answer

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