diff --git a/css/reference-field.css b/css/reference-field.css index 94d7395..9271470 100644 --- a/css/reference-field.css +++ b/css/reference-field.css @@ -9,13 +9,20 @@ width: 85%; } -.field--widget-entity-reference-autocomplete .reference-edit-link, -.field--widget-select2-entity-reference:has(.reference-edit-link) .reference-edit-link { +.field--widget-entity-reference-autocomplete .reference-edit-link { height: 100%; margin-top: 1.6rem; margin-bottom: 0; } +.field--widget-select2-entity-reference:has(.reference-edit-link) .dropbutton-wrapper { + margin-top: 2.2rem; +} + +.field--widget-select2-entity-reference:has(.reference-edit-link) .reference-edit-link .default a { + pointer-events: none; +} + .layout-region--node-secondary .field--widget-entity-reference-autocomplete { align-items: center; } diff --git a/entity_reference_edit_link.module b/entity_reference_edit_link.module index 60eee19..206fb14 100644 --- a/entity_reference_edit_link.module +++ b/entity_reference_edit_link.module @@ -5,6 +5,7 @@ * This is the module to create a drop-down menu for the core toolbar. */ +use Drupal\Core\Field\EntityReferenceFieldItemList; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Link; use Drupal\Core\Render\Element; @@ -12,6 +13,7 @@ use Drupal\Core\Url; use Drupal\entity_reference_edit_link\Plugin\Field\FieldWidget\EntityReferenceEditLinkAutocompleteTagsWidget; use Drupal\entity_reference_edit_link\Plugin\Field\FieldWidget\EntityReferenceEditLinkAutocompleteWidget; use Drupal\node\NodeForm; +use Drupal\select2\Plugin\Field\FieldWidget\Select2EntityReferenceWidget; use Drupal\user\Entity\User; /** @@ -80,6 +82,8 @@ function entity_reference_edit_link_field_widget_info_alter(array &$info) { /** * Implements hook_field_widget_complete_form_alter(). + * + * @throws \Drupal\Core\Entity\EntityMalformedException */ function entity_reference_edit_link_field_widget_complete_form_alter(&$field_widget_complete_form, FormStateInterface $form_state, $context) { $widget = $context['widget'] ?? NULL; @@ -88,19 +92,83 @@ function entity_reference_edit_link_field_widget_complete_form_alter(&$field_wid if (!$widget || $widget->getPluginId() != 'select2_entity_reference') { return; } - + /** @var \Drupal\select2\Plugin\Field\FieldWidget\Select2EntityReferenceWidget $widget */ $entity = !empty($context['items']) ? $context['items']->entity : NULL; $user = User::load(\Drupal::currentUser()->id()); if (empty($entity) || !$entity->access('update', $user)) { return; } + $field_widget_complete_form += [ + '#attached' => [ + 'library' => ['entity_reference_edit_link/reference.field'], + ], + ]; + + $field_widget_complete_form['widget'] = [ + 'target_id' => $field_widget_complete_form['widget'], + '_link' => _entity_reference_edit_link_prepare_link($widget, $context['items'], $entity), + ]; +} + +/** + * Prepares a render array for the field edit link. + * + * @param \Drupal\select2\Plugin\Field\FieldWidget\Select2EntityReferenceWidget $widget + * Field widget object. + * @param \Drupal\Core\Field\EntityReferenceFieldItemList $items + * Field items. + * @param \Drupal\Core\Entity\EntityInterface $entity + * Entity for the single value field case. + * + * @return array + * Returns render array with the link. + * + * @throws \Drupal\Core\Entity\EntityMalformedException + */ +function _entity_reference_edit_link_prepare_link(Select2EntityReferenceWidget $widget, EntityReferenceFieldItemList $items, mixed $entity) { + // Check if the widget allow multiple values. + $multiple = $widget->getPluginDefinition()['multiple_values'] ?? FALSE; + // Prepares container for the link. $link = [ '#type' => 'container', '#attributes' => [ 'class' => ['reference-edit-link-wrapper form-item'], ], - 'link' => [ + ]; + // Build multiple links if field widget allows multiple values. + if ($multiple) { + // Create default link value. + $links['default'] = [ + 'title' => t('Edit'), + 'url' => Url::fromRoute('<none>'), + ]; + // Prepares link for the field values. + /** @var \Drupal\Core\Entity\EntityInterface $referencedEntity */ + foreach ($items->referencedEntities() as $referencedEntity) { + $links[$referencedEntity->id()] = [ + 'title' => $referencedEntity->label(), + 'url' => $referencedEntity->toUrl('edit-form'), + 'attributes' => [ + 'target' => '_blank', + ], + ]; + } + + // Render multiple links as a dropbutton. + $link['link'] = [ + '#type' => 'dropbutton', + '#dropbutton_type' => 'small', + '#links' => $links, + '#attributes' => [ + 'class' => ['reference-edit-link'], + 'target' => '_blank', + ], + ]; + } + // Handle single value field. + else { + $link['link'] = [ '#type' => 'link', '#title' => t('Edit'), '#url' => $entity->toUrl('edit-form'), @@ -108,19 +176,10 @@ function entity_reference_edit_link_field_widget_complete_form_alter(&$field_wid 'class' => ['button reference-edit-link'], 'target' => '_blank', ], - ], - ]; - - $field_widget_complete_form += [ - '#attached' => [ - 'library' => ['entity_reference_edit_link/reference.field'], - ], - ]; + ]; + } - $field_widget_complete_form['widget'] = [ - 'target_id' => $field_widget_complete_form['widget'], - '_link' => $link, - ]; + return $link; } /**