Skip to content

Instantly share code, notes, and snippets.

@MikeNGarrett
Last active November 2, 2022 10:04
Show Gist options
  • Select an option

  • Save MikeNGarrett/8db8f0ac2a109a17bff5762e0aa2100c to your computer and use it in GitHub Desktop.

Select an option

Save MikeNGarrett/8db8f0ac2a109a17bff5762e0aa2100c to your computer and use it in GitHub Desktop.

Revisions

  1. MikeNGarrett renamed this gist Nov 2, 2022. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. MikeNGarrett renamed this gist Nov 2, 2022. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. MikeNGarrett created this gist Nov 2, 2022.
    32 changes: 32 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    <?php
    use Drupal\Core\Form\FormStateInterface;

    function hook_field_widget_complete_entity_reference_autocomplete_form_alter(&$field_widget_complete_form, $form_state, $context)
    {

    if($field_widget_complete_form['widget']['#field_name'] !== 'field_test') {
    return;
    }
    $storage = Drupal::getContainer()->get('entity_type.manager')->getStorage('node');
    $nids = $storage->getQuery();

    // Gather published artist nodes and sort by title
    $nids = $nids->condition('type', 'article')
    ->condition('status', 1)
    ->range(1, 10)
    ->sort('title')
    ->execute();
    if (!$nids) {
    return false;
    }

    $nodes = $storage->loadMultiple($nids);
    $temp = $field_widget_complete_form['widget'][0];
    foreach($nodes as $node) {
    $temp['target_id']['#default_value'] = $node;
    $field_widget_complete_form['widget'][] = $temp;
    }
    $field_widget_complete_form['widget'][] = $field_widget_complete_form['widget'][0];
    unset($field_widget_complete_form['widget'][0]);

    }