Score:0

How do I add a field to a node form?

km flag

The goal is to set a default counter, but let people override it. The counter should be the biggest value of the field plus 1.

Using an event subscriber, FORM ALTER event.

public static function getSubscribedEvents(): array {
  return [
    HookEventDispatcherInterface::FORM_ALTER => 'setDefaultCbid',
    HookEventDispatcherInterface::ENTITY_PRE_SAVE => 'checkCbiRaceCondition'
  ];
}


public function setDefaultCbid(FormAlterEvent $event) {

  $form_id = $event->getFormId();
  if ($form_id !== 'node_bond_edit_form') {
    return;
  }

  $result = \Drupal::database()
    ->query('select max(field_bond_cbid_value) from {node__field_bond_cbid}')
    ->fetchField();

  if ($result) {
    $cbid = $result + 1;
  }
  else {
    $cbid = 1;
  }

  $form = &$event->getForm();
  $form_already_alterered = false;
  if (array_key_exists('field_bond_cbid_default', $form)) {
    $form_already_alterered = TRUE;
  }
  $form['field_bond_cbid_default'] = [
    '#title' => $this->t('default cbid'),
    '#type' => 'number',
    '#value' => $cbid,
  ];

  if (!$form_already_alterered) {
    $form['field_bond_cbid']['widget'][0]['value']['#default_value'] = $cbid;
  }
}

This works fine. The value is set and the "default" field visible in the adjusted form

New field is here

What happens if a second person edits another node and increments the counter whINCORRECTCODEile the form is displayed? This is where field_bond_cbid_default is not available on the entity

public function checkCbiRaceCondition(EntityPresaveEvent $event) {   

  $bond = $event->getEntity();
  if ($bond->bundle() !== 'bond') {
  return;
}

$enteredValue = $bond->get('field_bond_cbid')->getString();
$defaultCbid = $bond->get('field_bond_cbid_default')->getString();
if ($enteredValue != $defaultCbid) {

Field not available

How do I access the default value?

Score:2
km flag

The form alter event is the wrong event for checking data. Instead of trying to deal with the form, use the backing entity.

On form alter event add a submit handler

$form['actions']['submit']['#submit'][]  = [$this, 'checkCbid'];

Check that the value is the latest on submit

public function checkCbiRaceCondition(&$form, FormState &$form_state) {
  $enteredValue = $form_state->getValue('field_bond_cbid')[0]['value'];
  $defaultCbid = $form['field_bond_cbid']['widget'][0]['value']['#default_value'];
  if (intval($enteredValue) != $defaultCbid) {
    return;
  }

  $result = \Drupal::database()
    ->query('select max(field_bond_cbid_value) from {node__field_bond_cbid}')
    ->fetchField();

  if ($result) {
    $cbid = $result +1;
  } else {
    $cbid = 1;
  }

  $form_state->set('field_bond_cbid', $cbid);
}
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.