0

I have a Content Type called Licence which has two fields, field_product and field_billing_cycle. Both are Entity Reference fields, using the select Widget. The product content type has a multi value entity reference that links to billing types. So each product has its own billing cycle with different prices and lengths etc.

When editing/creating a licence, I want to show only the billing cycles available for the product selected. I have created an entity reference view and set it all up so that it take a conditional filter arguements (The view seems to work, i set the contextual filter to a product id and it shows the right cycles). The only thing left now is to put it all together so that when the select list is changed, the billing cycles are updated but I cant seem to get the events right. Here is what i have so far:

function mymodule_form_node_licence_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  $form['field_start_date']['widget'][0]['value']['#ajax'] = [
    'callback' => 'mymodule_update_end_date',
    'event' => 'change',
  ];

  $form['field_product']['widget'][0]['value']['#ajax'] = [
    'callback' => 'mymodule_update_billing_cycles',
    'event' => 'change',
  ];

}

function mymodule_update_end_date($form, $form_state) {

  $start_date = $form['field_start_date']['widget'][0]['value']['#value']['object']->getPhpDateTime();
  // Calculate end date (add two months).
  $end_date = $start_date->modify('+2 month')->format('Y-m-d');

  $response = new AjaxResponse();
  $selector = '#edit-field-end-date-0-value-date';
  $method = 'val';
  $arguments = [$end_date];
  $response->addCommand(new InvokeCommand($selector, $method, $arguments));
  //$response->addCommand(new AlertCommand('Blah'));
  return $response;

}

function mymodule_update_billing_cycles($form, $form_state) {
   $response = new AjaxResponse();
   $response->addCommand(new AlertCommand("This is a test"));
   return $response;
}

The code for modifying the end date when the start date is changed is firing but the product change is not!

I want to get to the point where when the product is set, the cycles are updated. When the cycle is set, it updates the end date based on the cycle length added to the start date field. If the start date field is then changed it should update again based on the selected cycle but I am stuck. I have googled and googled but not managed to get it to work. Where am i going wrong?

***** Updated *****

Ok, so i have worked out why the event wasn't firing and have fixed it, the function above is now working. For anyone having a similar issue, it appears i should have added the ajax handler differently for the Select list:

function mymodule_form_node_licence_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  $form['field_start_date']['widget'][0]['value']['#ajax'] = [
    'callback' => 'mymodule_update_end_date',
    'event' => 'change',
  ];

  $form['field_product']['widget']['#ajax'] = [
    'callback' => 'mymodule_update_billing_cycles',
    'event' => 'change',
    'progress' => [
      'type' => 'throbber',
      'message' => 'Please Wait...',]
  ];

}

So now I am onto the next problem... How do i refresh the Entity Reference View, passing in the newly selected product id??

  • I'm not entirely sure I understood your structure correctly, but I think you should look into Paragraph module to create a "grouped reference", or Inline entity form drupal.org/project/inline_entity_form. I haven't tried them this way, but investigating down that line would be my first step. – prkos Sep 4 at 16:11
  • Thanks @prkos I have had to change my structure so that the Billing Cycle has a reference to the product on it, instead of the Billing cycles being created ON the product. Then I was able to use the Business Rules Module to create a "field dependant view" which works a treat. For anyone else looking to achieve a similar thing, there is a guide here drupal.org/docs/8/modules/business-rules/advanced-usage/… – cjsmith87 Sep 4 at 19:48

Your Answer

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

Browse other questions tagged or ask your own question.