Skip to content

Instantly share code, notes, and snippets.

@webflo
Created December 14, 2016 15:33
Show Gist options
  • Save webflo/5ce757306687b1438203ecb8aeb3f466 to your computer and use it in GitHub Desktop.
Save webflo/5ce757306687b1438203ecb8aeb3f466 to your computer and use it in GitHub Desktop.
<?php
/**
* @file
* Contains \Drupal\sdag_media\Plugin\Field\FieldWidget\EntityReferenceDialogWidget.
*/
namespace Drupal\sdag_media\Plugin\Field\FieldWidget;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
/**
* Plugin implementation of the 'sdag_media_dialog' widget.
*
* @FieldWidget(
* id = "sdag_media_dialog",
* label = @Translation("Media resource selector"),
* field_types = {
* "entity_reference",
* "entity_reference_metadata"
* },
* settings = {
* "progress_indicator" = "throbber"
* }
* )
*/
class EntityReferenceDialogWidget extends WidgetBase {
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$field_name = $this->fieldDefinition->getName();
$parents = $form['#parents'];
$element['#field_name'] = $field_name;
// Load the items for form rebuilds from the field state as they might not be
// in $form_state['values'] because of validation limitations.
$field_state = static::getWidgetState($parents, $field_name, $form_state);
if (isset($field_state['items'])) {
$items->setValue($field_state['items']);
}
$uuid = \Drupal::service('uuid')->generate();
$element['#type'] = 'details';
$element['#tree'] = TRUE;
$element['#collapsible'] = TRUE;
$element['#collapsed'] = FALSE;
$element['#attributes']['id'] = 'field-widget-' . $uuid;
$element['#attributes']['data-resource-widget'] = $uuid;
/*
$element['#attached']['css'][] = array(
'type' => 'file',
'data' => drupal_get_path('module', 'sdag_media') . '/css/sdag_media.widget.css',
);
*/
// $element['#type'] = 'container';
// $element['#attributes']['class'][] = 'form-item';
$element['#attributes']['class'][] = 'clearfix';
$element['resource'] = array(
'#type' => 'hidden',
'#value' => $items->offsetGet($delta)->target_id,
'#attributes' => array(
'data-resource-id' => 1,
),
);
$element['item'] = array(
'#type' => 'container',
'#attributes' => array(
'class' => array(
'form-item',
'clearfix',
),
),
);
if ($items->offsetGet($delta)->entity) {
$preview = entity_view($items->offsetGet($delta)->entity, 'preview');
$element['item']['preview']['#type'] = 'container';
$element['item']['preview']['#attributes']['class'][] = 'widget-preview';
$element['item']['preview']['entity'] = array(
'#markup' => render($preview),
);
}
$element['item']['operations']['#type'] = 'container';
$element['item']['operations']['#attributes']['class'][] = 'widget-operations';
$element['item']['operations']['search'] = array(
'#type' => 'link',
'#title' => t('Select'),
'#value' => t('Select'),
'#url' => Url::fromRoute('view.sdag_media_resource_dialog.entity_reference_dialog_widget', array('arg_0' => $uuid)),
'#options' => array(
'query' => array(
'resource-widget' => $uuid,
),
),
'#attributes' => array(
'class' => array('button', 'use-ajax'),
'data-accepts' => 'application/vnd.drupal-modal',
'data-dialog-options' => json_encode(array(
'height' => '500',
'width' => '90%',
'title' => 'Select a image',
)),
),
'#limit_validation_errors' => array(array_merge($parents, array($field_name))),
);
$element['item']['operations']['remove'] = array(
'#type' => 'submit',
'#name' => 'field-widget--' . $field_name . '--remove',
'#value' => t('Remove'),
'#ajax' => array(
'callback' => '\Drupal\sdag_media\Plugin\Field\FieldWidget\EntityReferenceDialogWidget::updateWidget',
'wrapper' => 'field-widget-' . $uuid,
),
'#attributes' => array(
'data-resource-remove' => 1,
),
'#submit' => array('\Drupal\sdag_media\Plugin\Field\FieldWidget\EntityReferenceDialogWidget::operationRemoveSubmit'),
'#limit_validation_errors' => array(array_merge($parents, array($field_name))),
);
$element['item']['operations']['update'] = array(
'#type' => 'submit',
'#name' => 'field-widget--' . $field_name . '--update',
'#value' => t('Update Preview'),
'#ajax' => array(
'callback' => '\Drupal\sdag_media\Plugin\Field\FieldWidget\EntityReferenceDialogWidget::updateWidget',
'wrapper' => 'field-widget-' . $uuid,
),
'#attributes' => array(
'data-resource-update' => 1,
'class' => array('js-hide'),
),
'#submit' => array('\Drupal\sdag_media\Plugin\Field\FieldWidget\EntityReferenceDialogWidget::operationUpdatePreview'),
'#limit_validation_errors' => array(array_merge($parents, array($field_name))),
);
return $element;
}
public static function operationRemoveSubmit($form, FormStateInterface $form_state) {
$trigger_element = $form_state->getTriggeringElement();
$parents = $trigger_element['#array_parents'];
$parents = array_slice($parents, 0, -3);
$element = NestedArray::getValue($form, $parents);
$field_name = $element['#field_name'];
$parents = $element['#field_parents'];
$field_state = static::getWidgetState($parents, $field_name, $form_state);
$field_state['items'] = array();
$field_state['items_count'] = count($field_state['items']);
static::setWidgetState($parents, $field_name, $form_state, $field_state);
$form_state->setRebuild(TRUE);
}
public static function operationUpdatePreview($form, FormStateInterface $form_state) {
// Determine whether it was the upload or the remove button that was clicked,
// and set $element to the managed_file element that contains that button.
$parents = $form_state->getTriggeringElement()['#array_parents'];
$parents = array_slice($parents, 0, -3);
$element = NestedArray::getValue($form, $parents);
$field_name = $element['#field_name'];
$parents = $element['#field_parents'];
$field_state = static::getWidgetState($parents, $field_name, $form_state);
$field_state['items'] = array();
$form_state_parents = array_merge($parents, array($field_name));
$values = NestedArray::getValue($form_state->getUserInput(), $form_state_parents);
foreach ($values as $delta => $item) {
$field_state['items'][$delta] = array(
'target_id' => $item['resource'],
'resource' => $item['resource'],
);
}
$field_state['items_count'] = count($field_state['items']);
static::setWidgetState($parents, $field_name, $form_state, $field_state);
$form_state->setRebuild();
}
public static function updateWidget($form, FormStateInterface $form_state) {
$parents = $form_state->getTriggeringElement()['#array_parents'];
$parents = array_slice($parents, 0, -3);
$element = NestedArray::getValue($form, $parents);
return $element;
}
/**
* {@inheritdoc}
*/
public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
foreach ($values as &$value) {
$value['target_id'] = $value['resource'];
};
return $values;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment