Commit e8ab0b26 authored by catch's avatar catch

Issue #2293723 by yched: Generate lighter $form[$field] structures.

parent 03cc400b
......@@ -283,11 +283,6 @@ protected function formSingleElement(FieldItemListInterface $items, $delta, arra
$entity = $items->getEntity();
$element += array(
'#entity_type' => $entity->getEntityTypeId(),
'#bundle' => $entity->bundle(),
'#entity' => $entity,
'#field_name' => $this->fieldDefinition->getName(),
'#language' => $items->getLangcode(),
'#field_parents' => $form['#parents'],
// Only the first widget should be required.
'#required' => $delta == 0 && $this->fieldDefinition->isRequired(),
......
......@@ -79,11 +79,6 @@ public function settingsSummary();
* The order of this item in the array of subelements (0, 1, 2, etc).
* @param array $element
* A form element array containing basic properties for the widget:
* - #entity_type: The name of the entity the field is attached to.
* - #bundle: The name of the field bundle the field is contained in.
* - #entity: The entity the field is attached to.
* - #field_name: The name of the field.
* - #language: The language the field is being edited in.
* - #field_parents: The 'parents' space for the field in the form. Most
* widgets can simply overlook this property. This identifies the
* location where the field values are placed within
......
......@@ -28,18 +28,13 @@ class CommentWidget extends WidgetBase {
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, array &$form_state) {
$field = $this->fieldDefinition;
$entity = $items->getParent();
// Get default value from the field instance.
$field_default_values = $this->fieldDefinition->getDefaultValue($entity);
$status = $items->status;
$entity = $items->getEntity();
$element['status'] = array(
'#type' => 'radios',
'#title' => t('Comments'),
'#title_display' => 'invisible',
'#default_value' => $status,
'#default_value' => $items->status,
'#options' => array(
CommentItemInterface::OPEN => t('Open'),
CommentItemInterface::CLOSED => t('Closed'),
......@@ -58,7 +53,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
// If the entity doesn't have any comments, the "hidden" option makes no
// sense, so don't even bother presenting it to the user unless this is the
// default value widget on the field settings form.
if ($element['#field_parents'] != array('default_value_input') && !$entity->get($field->getName())->comment_count) {
if ($element['#field_parents'] != array('default_value_input') && !$items->comment_count) {
$element['status'][CommentItemInterface::HIDDEN]['#access'] = FALSE;
// Also adjust the description of the "closed" option.
$element['status'][CommentItemInterface::CLOSED]['#description'] = t('Users cannot post comments.');
......@@ -67,6 +62,9 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
// second column on wide-resolutions), place the field as a details element
// in this tab-set.
if (isset($form['advanced'])) {
// Get default value from the field instance.
$field_default_values = $this->fieldDefinition->getDefaultValue($entity);
$element += array(
'#type' => 'details',
// Open the details when the selected value is different to the stored
......@@ -74,7 +72,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
'#open' => ($items->status != $field_default_values[0]['status']),
'#group' => 'advanced',
'#attributes' => array(
'class' => array('comment-' . drupal_html_class($element['#entity_type']) . '-settings-form'),
'class' => array('comment-' . drupal_html_class($entity->getEntityTypeId()) . '-settings-form'),
),
'#attached' => array(
'library' => array('comment/drupal.comment'),
......
......@@ -540,7 +540,6 @@ function testFieldFormAccess() {
$form_state = form_state_defaults();
$display->buildForm($entity, $form, $form_state);
$this->assertEqual($form[$field_name_no_access]['widget'][0]['value']['#entity_type'], $entity_type, 'The correct entity type is set in the field structure.');
$this->assertFalse($form[$field_name_no_access]['#access'], 'Field #access is FALSE for the field without edit access.');
// Display creation form.
......
......@@ -159,8 +159,8 @@ protected function formMultipleElements(FieldItemListInterface $items, array &$f
$elements['#title'] = $title;
$elements['#description'] = $description;
$elements['#field_name'] = $element['#field_name'];
$elements['#language'] = $element['#language'];
$elements['#field_name'] = $field_name;
$elements['#language'] = $items->getLangcode();
$elements['#display_field'] = (bool) $this->getFieldSetting('display_field');
// The field settings include defaults for the field type. However, this
// widget is a base class for other widgets (e.g., ImageWidget) that may
......@@ -218,6 +218,8 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
// Allows this field to return an array instead of a single value.
'#extended' => TRUE,
// Add properties needed by value() and process() methods.
'#field_name' => $this->fieldDefinition->getName(),
'#entity_type' => $items->getEntity()->getEntityTypeId(),
'#display_field' => (bool) $field_settings['display_field'],
'#display_default' => $field_settings['display_default'],
'#description_field' => $field_settings['description_field'],
......
......@@ -87,8 +87,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
// Post-process the title field to make it conditionally required if URL is
// non-empty. Omit the validation on the field edit form, since the field
// settings cannot be saved otherwise.
$is_field_edit_form = ($element['#entity'] === NULL);
if (!$is_field_edit_form && $this->getFieldSetting('title') == DRUPAL_REQUIRED) {
if (empty($form_state['default_value_widget']) && $this->getFieldSetting('title') == DRUPAL_REQUIRED) {
$element['#element_validate'][] = array($this, 'validateTitle');
}
......
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