Bit stuck on trying to just some hooks in a custom module

by gibbo   Last Updated May 15, 2019 16:07 PM - source

I am trying to write an auto increment in a custom module that will check how many records exist for a specific content type (responses) and then add to a custom field the count + 1.

Response 1, Response 2, etc...

Ultimately I want to count how many entities of the type responses exist that are also related to a parent entity that is referenced when I save the form, but at this stage I can't even get it to count and populate my custom field on save.

I've hunted around and ended up with the following code (Im learning in Drupal 8)... can anyone see what I am doing wrong please. My work flow is just to create a new entity called responses and press save.

Thanks in advance for any assistance

function myschool_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) 
//Add a custom submit handler
if($form_id = "responses_form"){
 $form['#submit'][] = 'myschool_custom_submit';

// Custom submit handler to assign the custom field value
myschool_custom_submit(&$form, &$form_state)
  $form_state['values']['field_response_number']['und'][0]['value'] = myschool_preprint_increment();


// Custom function to increment by one
function myschool_preprint_increment()
// Content type machine name
$type = "responses";
  $nodes = node_load_multiple(array(), array('type' => $type));

// STILL TO DO – Add a check for reference ID = Parent Entity ID and count from that
  if (!empty($nodes)) {
    $pre_print_number = "Response";
    $pre_print_number .= count($nodes) + 1;
  else {
    $pre_print_number = "Response1"; //First response if empty


  return $pre_print_number;
Tags : hooks

Related Questions

How to find hooks being called by a Drupal page?

Updated April 03, 2015 21:20 PM

Modify text of error message for required field

Updated April 22, 2015 20:03 PM

Where are hooks stored/cached?

Updated April 13, 2015 20:03 PM