When debugging to figure out why a prepopulate URL isn't working, or for future maintainers, it would be really useful if the various steps in _prepopulate_request_walk() explained what they were doing.

Something like this:

      $widget = $form['widget'];
      if (empty($widget) || (isset($widget[0]['value']['#type']) && $widget[0]['value']['#type'] == 'markup')) {
// It's a thingy widget.
        $form['widget'][0]['value']['#value'] = check_plain($request_slice);
      }
      else {
        if (isset($form['widget'][0]['value'])) {
// It's a foobar.
          $form['widget'][0]['value']['#value'] = $request_slice;
        }
        else {
          if (isset($form['widget'][0]['target_id'])) {
            // Referenced element: look for name ? Title (id)
            if (is_numeric($request_slice) && ($target_node = \Drupal\node\Entity\Node::load($request_slice)) && $target_node->access('view')) {
// If the given prepopulate value is only an ID, load the node title.
              $form['widget'][0]['target_id']['#value'] = $target_node->getTitle() . ' (' . $request_slice . ')';
            }
            else {
// Otherwise, assume we've been given the title as well as the ID in the right format.
              $form['widget'][0]['target_id']['#value'] = $request_slice;
            }
          }
        }
      }
      $type = (isset($widget['#type'])) ? $widget['#type'] : NULL;
      if ($type == 'select') {
        $form['widget']['#value'] = $request_slice;
      }

Comments

joachim created an issue. See original summary.

heddn’s picture

Status: Active » Closed (outdated)