You are here

function editableviews_handler_field_entity_metadata_property::edit_form in Editable Views 7

Add the edit form for the field.

File

handlers/editableviews_handler_field_entity_metadata_property.inc, line 147

Class

editableviews_handler_field_entity_metadata_property
Field handler for editing an entity metadata property.

Code

function edit_form($entity_type, $entity, &$element, &$form_state) {

  // Something weird in Views admin UI causes us to come here (because the
  // style plugin gets rendered) without the options set on this handler. This
  // then causes an AJAX error because further down we access a metadata
  // wrapper with no property. I have no time to go chasing this right now, so
  // for now, just bail here if we're not properly set up. Doing this appears
  // to have no adverse or visible side effects.
  if (empty($this->options['property'])) {
    return;
  }
  $wrapper = entity_metadata_wrapper($entity_type, $entity);

  // Get the info for the property we are working with. We only need to get
  // this once.
  if (!isset($this->property_info)) {
    $this->property_info = $wrapper
      ->getPropertyInfo($this->options['property']);
  }
  if (isset($this->property_info['options list'])) {

    // Special case: if the property has an 'options list' defined, we can
    // show a select form element of options.
    $this
      ->edit_form_element_select($element, $form_state, $wrapper);
  }
  else {

    // The type of form element we add depends on the type of the property.
    // This is just a best guess.
    // TODO: add an option to override this?
    switch ($this->property_info['type']) {
      case 'boolean':
        $this
          ->edit_form_element_checkbox($element, $form_state, $wrapper);
        break;
      default:
        $this
          ->edit_form_element_textfield($element, $form_state, $wrapper);
        break;
    }
  }

  // Set the title property.
  if ($this->options['form_use_label']) {
    $element[$this->options['property']]['#title'] = $this->options['label'];
  }
  else {
    $element[$this->options['property']]['#title'] = check_plain($this->property_info['label']);
  }
}