X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fmodules%2Fcontrib%2Fentity_browser%2Fsrc%2FElement%2FEntityBrowserElement.php;fp=web%2Fmodules%2Fcontrib%2Fentity_browser%2Fsrc%2FElement%2FEntityBrowserElement.php;h=00840aa2b0cd2d380983201258673f6c9215fe2a;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/web/modules/contrib/entity_browser/src/Element/EntityBrowserElement.php b/web/modules/contrib/entity_browser/src/Element/EntityBrowserElement.php new file mode 100644 index 000000000..00840aa2b --- /dev/null +++ b/web/modules/contrib/entity_browser/src/Element/EntityBrowserElement.php @@ -0,0 +1,254 @@ + TRUE, + '#tree' => TRUE, + '#cardinality' => static::CARDINALITY_UNLIMITED, + '#selection_mode' => static::SELECTION_MODE_APPEND, + '#process' => [[$class, 'processEntityBrowser']], + '#default_value' => [], + '#entity_browser_validators' => [], + '#attached' => ['library' => ['entity_browser/common']], + ]; + } + + /** + * Get selection mode options. + * + * @return array + * Selection mode options. + */ + public static function getSelectionModeOptions() { + return [ + static::SELECTION_MODE_APPEND => t('Append to selection'), + static::SELECTION_MODE_PREPEND => t('Prepend selection'), + static::SELECTION_MODE_EDIT => t('Edit selection'), + ]; + } + + /** + * Check whether entity browser should be available for selection of entities. + * + * @param string $selection_mode + * Used selection mode. + * @param int $cardinality + * Used cardinality. + * @param int $preselection_size + * Preseletion size, if it's available. + * + * @return bool + * Returns positive if entity browser can be used. + */ + public static function isEntityBrowserAvailable($selection_mode, $cardinality, $preselection_size) { + if ($selection_mode == static::SELECTION_MODE_EDIT) { + return TRUE; + } + + $cardinality_exceeded = + $cardinality != static::CARDINALITY_UNLIMITED + && $preselection_size >= $cardinality; + + return !$cardinality_exceeded; + } + + /** + * Render API callback: Processes the entity browser element. + */ + public static function processEntityBrowser(&$element, FormStateInterface $form_state, &$complete_form) { + /** @var \Drupal\entity_browser\EntityBrowserInterface $entity_browser */ + if (is_string($element['#entity_browser'])) { + $entity_browser = EntityBrowser::load($element['#entity_browser']); + } + else { + $entity_browser = $element['#entity_browser']; + } + + // Propagate selection if edit selection mode is used. + $entity_browser_preselected_entities = []; + if ($element['#selection_mode'] === static::SELECTION_MODE_EDIT) { + $entity_browser->getSelectionDisplay()->checkPreselectionSupport(); + + $entity_browser_preselected_entities = $element['#default_value']; + } + + $default_value = implode(' ', array_map( + function (EntityInterface $item) { + return $item->getEntityTypeId() . ':' . $item->id(); + }, + $entity_browser_preselected_entities + )); + $validators = array_merge( + $element['#entity_browser_validators'], + ['cardinality' => ['cardinality' => $element['#cardinality']]] + ); + + $display = $entity_browser->getDisplay(); + $display->setUuid(sha1(implode('-', array_merge([$complete_form['#build_id']], $element['#parents'])))); + $element['entity_browser'] = [ + '#eb_parents' => array_merge($element['#parents'], ['entity_browser']), + ]; + $element['entity_browser'] = $display->displayEntityBrowser( + $element['entity_browser'], + $form_state, + $complete_form, + [ + 'validators' => $validators, + 'selected_entities' => $entity_browser_preselected_entities, + ] + ); + + $hidden_id = Html::getUniqueId($element['#id'] . '-target'); + $element['entity_ids'] = [ + '#type' => 'hidden', + '#id' => $hidden_id, + // We need to repeat ID here as it is otherwise skipped when rendering. + '#attributes' => ['id' => $hidden_id, 'class' => ['eb-target']], + '#default_value' => $default_value, + ]; + + $element['#attached']['drupalSettings']['entity_browser'] = [ + $entity_browser->getDisplay()->getUuid() => [ + 'cardinality' => $element['#cardinality'], + 'selection_mode' => $element['#selection_mode'], + 'selector' => '#' . $hidden_id, + ], + ]; + + return $element; + } + + /** + * {@inheritdoc} + */ + public static function valueCallback(&$element, $input, FormStateInterface $form_state) { + if ($input === FALSE) { + return $element['#default_value'] ?: []; + } + + $entities = []; + if ($input['entity_ids']) { + $entities = static::processEntityIds($input['entity_ids']); + } + + return ['entities' => $entities]; + } + + /** + * Processes entity IDs and gets array of loaded entities. + * + * @param array|string $ids + * Processes entity IDs as they are returned from the entity browser. They + * are in [entity_type_id]:[entity_id] form. Array of IDs or a + * space-delimited string is supported. + * + * @return \Drupal\Core\Entity\EntityInterface[] + * Array of entity objects. + */ + public static function processEntityIds($ids) { + if (!is_array($ids)) { + $ids = array_filter(explode(' ', $ids)); + } + + return array_map( + function ($item) { + list($entity_type, $entity_id) = explode(':', $item); + return \Drupal::entityTypeManager()->getStorage($entity_type)->load($entity_id); + }, + $ids + ); + } + + /** + * Processes entity IDs and gets array of loaded entities. + * + * @param string $id + * Processes entity ID as it is returned from the entity browser. ID should + * be in [entity_type_id]:[entity_id] form. + * + * @return \Drupal\Core\Entity\EntityInterface + * Entity object. + */ + public static function processEntityId($id) { + $return = static::processEntityIds([$id]); + return current($return); + } + +}