Commit 07ebe300 authored by DamienMcKenna's avatar DamienMcKenna Committed by fago
Browse files

Issue #3185260: Make Feld Collection 8.x-3.x compatible with Drupal 9

parent 3a75aa58
......@@ -2,6 +2,7 @@ name: Field Collection
description: 'Provides a field collection field, to which any number of fields can be attached.'
package: Field types
core: 8.x
core_version_requirement: ^8 || ^9
configure: field_collection.overview_field_collections
type: module
dependencies:
......
......@@ -43,7 +43,7 @@ function field_collection_field_storage_config_insert(FieldStorageConfigInterfac
->execute();
if (!$field_collection_exists) {
$values = ['label' => $field->getName(), 'id' => $field->getName()];
$field_collection = \Drupal::service('entity.manager')->getStorage('field_collection')->create($values);
$field_collection = \Drupal::service('entity_type.manager')->getStorage('field_collection')->create($values);
$field_collection->enforceIsNew();
$field_collection->save();
}
......
......@@ -18,6 +18,6 @@ services:
field_collection.normalizer.field_collection_item:
class: Drupal\field_collection\Normalizer\FieldCollectionItemNormalizer
arguments: ['@entity.manager']
arguments: ['@entity_type.manager']
tags:
- { name: normalizer, priority: 20 }
......@@ -32,7 +32,7 @@ class FieldCollectionItemController extends ControllerBase {
public function add(FieldCollection $field_collection, $host_type, $host_id) {
$host = $this->entityTypeManager()->getStorage($host_type)->load($host_id);
if (_field_collection_field_item_list_full($host->{$field_collection->id()})) {
drupal_set_message(t('This field is already full.'), 'error');
$this->messenger()->addError(t('This field is already full.'), 'error');
return ['#markup' => 'Can not add to an already full field.'];
}
else {
......@@ -120,7 +120,7 @@ class FieldCollectionItemController extends ControllerBase {
->getStorage('field_collection_item')
->loadRevision($field_collection_item_revision);
$field_collection_item_view_controller = new EntityViewController($this->entityManager(), \Drupal::service('renderer'));
$field_collection_item_view_controller = new EntityViewController($this->entityTypeManager(), \Drupal::service('renderer'));
$page = $field_collection_item_view_controller
->view($field_collection_item);
......
......@@ -361,7 +361,7 @@ class FieldCollectionItem extends ContentEntityBase implements FieldCollectionIt
// Add the field collection item to its host.
if ($create_link) {
if (_field_collection_field_item_list_full($entity->{$this->bundle()})) {
drupal_set_message(t('Field is already full.'), 'error');
$this->messenger()->addError(t('Field is already full.'), 'error');
}
else {
$bundle = $this->bundle();
......
......@@ -23,7 +23,7 @@ class FieldCollectionForm extends EntityForm {
// There should be no way to attempt to add a field collection through
// this form but set up a message for it just in case.
$form['#title'] = $this->t('Add field collection');
drupal_set_message(t('To add a field collection create a field of type field collection on the host entity type.'));
$this->messenger()->addMessage(t('To add a field collection create a field of type field collection on the host entity type.'));
}
else {
$form['#title'] = $this->t('Edit %label field collection', ['%label' => $field_collection->label()]);
......
......@@ -86,14 +86,14 @@ class FieldCollectionItemForm extends ContentEntityForm {
$messages = drupal_get_messages(NULL, false);
if (!isset($messages['warning']) && !isset($messages['error'])) {
drupal_set_message(t('Successfully added a @type.', ['@type' => $field_collection_item->bundle()]));
$this->messenger()->addMessage(t('Successfully added a @type.', ['@type' => $field_collection_item->bundle()]));
}
}
else {
$messages = drupal_get_messages(NULL, false);
if (!isset($messages['warning']) && !isset($messages['error'])) {
$field_collection_item->save();
drupal_set_message(t('Successfully edited %label.', ['%label' => $field_collection_item->label()]));
$this->messenger()->addMessage(t('Successfully edited %label.', ['%label' => $field_collection_item->label()]));
}
}
......@@ -104,7 +104,7 @@ class FieldCollectionItemForm extends ContentEntityForm {
else {
// In the unlikely case something went wrong on save, the block will be
// rebuilt and block form redisplayed.
drupal_set_message(t('The field collection item could not be saved.'), 'error');
$this->messenger()->addMessage(t('The field collection item could not be saved.'), 'error');
$form_state->setRebuild();
}
......
......@@ -49,7 +49,7 @@ class FieldCollectionItemDeleteForm extends ContentEntityConfirmFormBase {
$node_type_storage = $this->entityTypeManager->getStorage('field_collection');
$node_type = $node_type_storage->load($this->entity->bundle())->label();
drupal_set_message(t(
$this->messenger()->addMessage(t(
'@type %id has been deleted.',
[
'@type' => $node_type,
......
......@@ -10,6 +10,7 @@ use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Component\Utility\NestedArray;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Render\Element;
use Drupal\Core\Security\TrustedCallbackInterface;
use Drupal\field_collection\Entity\FieldCollectionItem;
/**
......@@ -23,7 +24,7 @@ use Drupal\field_collection\Entity\FieldCollectionItem;
* },
* )
*/
class FieldCollectionEmbedWidget extends WidgetBase {
class FieldCollectionEmbedWidget extends WidgetBase implements TrustedCallbackInterface {
/**
* {@inheritdoc}
......@@ -60,7 +61,7 @@ class FieldCollectionEmbedWidget extends WidgetBase {
static::setWidgetState($element['#field_parents'], $field_name, $form_state, $field_state);
$display = entity_get_form_display('field_collection_item', $field_name, 'default');
$display = \Drupal::service('entity_display.repository')->getFormDisplay('field_collection_item', $field_name, 'default');
$display->buildForm($field_collection_item, $element, $form_state);
if (empty($element['#required'])) {
......@@ -179,7 +180,7 @@ class FieldCollectionEmbedWidget extends WidgetBase {
$field_collection_item = $field_state['entity'][$element['#delta']];
$display = entity_get_form_display('field_collection_item', $field_name, 'default');
$display = \Drupal::service('entity_display.repository')->getFormDisplay('field_collection_item', $field_name, 'default');
$display->extractFormValues($field_collection_item, $element, $form_state);
// Now validate required elements if the entity is not empty.
......@@ -190,9 +191,7 @@ class FieldCollectionEmbedWidget extends WidgetBase {
if (isset($elements['#needs_validation'])) {
$is_countable = is_array($elements['#value']) || $elements['#value'] instanceof \Countable;
$is_empty_multiple = $is_countable && count($elements['#value']) == 0;
// @todo: replace Unicode::strlen() with mb_substr() per
// https://www.drupal.org/project/drupal/issues/2849669
$is_empty_string = (is_string($elements['#value']) && Unicode::strlen(trim($elements['#value'])) == 0);
$is_empty_string = (is_string($elements['#value']) && mb_strlen(trim($elements['#value'])) == 0);
$is_empty_value = ($elements['#value'] === 0);
$is_empty_option = (isset($elements['#options']['_none']) && $elements['#value'] == '_none');
......@@ -374,4 +373,13 @@ class FieldCollectionEmbedWidget extends WidgetBase {
$form_state->setRebuild();
}
/**
* Returns a list of trusted methods that can be used as callbacks.
*
* @return string[]
* Trusted methods.
*/
public static function trustedCallbacks() {
return ['renderRequired'];
}
}
......@@ -103,11 +103,11 @@ trait FieldCollectionTestTrait {
$inner_field->save();
entity_get_form_display('field_collection_item', $this->field_collection_name, 'default')
\Drupal::service('entity_display.repository')->getFormDisplay('field_collection_item', $this->field_collection_name, 'default')
->setComponent($this->inner_field_name, ['type' => 'number'])
->save();
entity_get_display('field_collection_item', $this->field_collection_name, 'default')
\Drupal::service('entity_display.repository')->getViewDisplay('field_collection_item', $this->field_collection_name, 'default')
->setComponent($this->inner_field_name, ['type' => 'number_decimal'])
->save();
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment