You are here

function editableviews_handler_field_field_edit::edit_form in Editable Views 7

Add the form element for this handler's field to the form.

We break this out into the handler to allow other handlers that work with non-FieldAPI fields (eg node title) to also provide a form element.

Parameters

$entity_type: The entity type.

$entity: The entity.

&$element: The partial form, at $form[ENTITY_ID]. This is passed by reference and should be altered in place.

&$form_state: The form state.

File

handlers/editableviews_handler_field_field_edit.inc, line 90

Class

editableviews_handler_field_field_edit
Field handler for toggling between rendered value and edit form element.

Code

function edit_form($entity_type, $entity, &$element, &$form_state) {
  list($entity_id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);

  // TODO: this prevents this method being static. This would mean we could
  // store class names in the form rather than handler objects, which would
  // considerably save on form cache size!
  $field_name = $this->definition['field_name'];
  $field_instance = field_info_instance($entity_type, $field_name, $bundle);

  // Because a View result can show entities of different bundles, it is
  // essential that we check the field actually exists on the current entity.
  // ctools_field_invoke_field() does actually check for this too, but that
  // only works when it's passed a field name rather than a whole instance.
  if (empty($field_instance)) {
    return;
  }

  // TODO: Faffy to have to keep doing this for the entity in each handler!!!!
  if (isset($entity->language)) {
    $langcode = field_valid_language($entity->language);
  }
  else {
    $langcode = field_valid_language(NULL);
  }
  if (!empty($this->options['suppress_label'])) {
    $field_instance['label'] = '';
  }
  if (!empty($this->options['suppress_description'])) {
    $field_instance['description'] = '';
  }

  // Only doctor the widget type if the option is set; otherwise the widget
  // type set for the entity's bundle will be used.
  if (!empty($this->options['widget_type'])) {
    $field_instance['widget']['type'] = $this->options['widget_type'];

    // We also need to set the module for the widget, in case this differs
    // from the module for the original widget, because this is used to
    // determine which module's hook_field_widget_form() gets invoked.
    $widget_info = field_info_widget_types();
    $widget_info = $widget_info[$this->options['widget_type']];
    $field_instance['widget']['module'] = $widget_info['module'];
  }

  // On new entities, force this to not be required, to allow the user to not
  // create the new entity.
  // Obviously, problems arise when there are multiple editable fields on
  // this entity, since the title *is* required if actually creating an
  // entity!
  // TODO: consider this thorny problem.
  if (!empty($entity->is_new)) {
    $field_instance['required'] = FALSE;
  }

  // If no language is provided use the default site language.
  $field_invoke_options = array(
    'language' => $langcode,
    'default' => TRUE,
  );
  $element += (array) ctools_field_invoke_field($field_instance, 'form', $entity_type, $entity, $element, $form_state, $field_invoke_options);
}