Problem/Motivation

Checklist elements are supposed to be build using the #callback attribute in checklist definition, however it isn't possible to pass additional variables into the callback since there is a straight up call call_user_func($definition['#callback']);.

Proposed resolution

Introduce a new attribute in the checklist definition #callback_arguments and pass these arguments through to the callback.

Remaining tasks

N/A

User interface changes

N/A

API changes

New optional checklist definition property #callback_arguments.

Data model changes

N/A

Release notes snippet

N/A

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Erik Frèrejean created an issue. See original summary.

Erik Frèrejean’s picture

Assigned: Erik Frèrejean » Unassigned
Status: Active » Needs review
FileSize
2.17 KB

Attached a patch and added a change record: https://www.drupal.org/node/3077055

Erik Frèrejean’s picture

FileSize
2.18 KB

Made a typo in the api file.

TravisCarden’s picture

Thank you for the contribution, @Erik Frèrejean. Can you help me understand the motivation for this change? What are you unable to do today that it would enable you to do?

Erik Frèrejean’s picture

Sure. First of all the change doesn't allow me to do anything I can't do without the patch, but in order to do so I need to rely on deprecated behavior, which for obvious reasons don't want to do ;).

Basically we've build a UI for the checklist api where certain users can create checklists on the fly by connecting a couple of custom entity types. In essence the implementation of hook_checklistapi_checklist_info look like:

function hook_checklistapi_checklist_info() {
  $info = [];
  foreach(Drupal::entityTypeManager()->getStorage('foobar')->loadMultiple() as $list) {
    $info[$list->uuid()] = [
      '#title' => $list->label(),
      '#description' => $list->getDescription(),
      '#path' => 'checklist/' . $list->id(),
      '#callback' => '_buildlistitems',
      '#callback_arguments' => [$list],
    ];
  }
}

function _buildlistitems(ListInterface $list) {
  $elements = [];

  foreach ($learning_path->elements->referencedEntities() as $element) {
    $elements[$element->uuid()] = [
      '#title' => $element->label(),
      '#description' => $element->getDescription(),
    ];
  }

  return $elements;
}

Our initial implementation has the loop defined in _buildlistitems straight in the info hook, but per the api documentation that behavior is deprecated. And since the content of the list is defined by the entity that is being converted it is needed to pass the entity through to the callback.

  • TravisCarden committed c33e5df on 8.x-1.x
    Issue #3077054 by Erik Frèrejean: Make callback arguments available...
TravisCarden’s picture

Status: Needs review » Postponed

That makes sense, @Erik Frèrejean. Committed. Thanks!

TravisCarden’s picture

Status: Postponed » Fixed

Oops. Meant to mark fixed, not postponed.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.