You are here

class EntityReferenceAutofillInstanceBehavior in Entity reference autofill 7

Class for handling module behavior.

Hierarchy

Expanded class hierarchy of EntityReferenceAutofillInstanceBehavior

1 string reference to 'EntityReferenceAutofillInstanceBehavior'
autofill.inc in plugins/behavior/autofill.inc

File

plugins/behavior/EntityReferenceAutofillInstanceBehavior.class.php, line 10
Entity reference behavior handler for autofill module.

View source
class EntityReferenceAutofillInstanceBehavior extends EntityReference_BehaviorHandler_Abstract {

  /**
   * Generate a settings form for this handler.
   */
  public function settingsForm($field, $instance) {

    // Current settings.
    $settings = $instance['settings']['behaviors']['autofill'];
    $widget = $instance['widget'];

    // Load available fields from instance bundle.
    $entity_type = $instance['entity_type'];
    $bundle_name = $instance['bundle'];
    $bundle_info = field_info_instances($entity_type, $bundle_name);

    // Field target metadata.
    $field_map = field_info_field_map();
    $target_type = $field['settings']['target_type'];
    $target_bundles = isset($field['settings']['handler_settings']['target_bundles']);
    $target_bundles = $target_bundles ? $field['settings']['handler_settings']['target_bundles'] : array();

    // Skip autofill for the reference field.
    unset($bundle_info[$field['field_name']]);

    // Create checkboxes options for available fields.
    $field_options = array();
    foreach ($bundle_info as $field_name => $field_info) {
      if (!empty($field_map[$field_name]['bundles'][$target_type])) {
        $available_bundles = $field_map[$field_name]['bundles'][$target_type];

        // Determine all targeted bundles that use this field.
        $option_bundles = empty($target_bundles) ? $available_bundles : array_intersect($target_bundles, $available_bundles);
        if (!empty($option_bundles)) {
          $field_options[$field_name] = t('@field_label (@field_name): <em>Available in bundle(s) @bundles</em>', array(
            '@field_label' => $field_info['label'],
            '@field_name' => $field_name,
            '@bundles' => implode(', ', $option_bundles),
          ));
        }
      }
    }
    $form['overwrite'] = array(
      '#type' => 'checkbox',
      '#title' => t('Overwrite existing data'),
      '#description' => t('Select if you want to overwrite fields that already have values. <br/><em><strong>NOTE:</strong> Disabling this is experimental and might not work 100%. If you experience issues with fields being overridden nonetheless, please report what field type and settings this occurs on in the modules issue queue on drupal.org</em>'),
      '#default_value' => isset($settings['overwrite']) ? $settings['overwrite'] : 1,
    );
    if (empty($field_options)) {
      $no_fields_found = t('There are no common fields between this bundle and its referenced entities.');
      $usage_instructions = t('To use autofill, you need to add instances of the same fields to its referenced bundles.');
      $form['no_fields'] = array(
        '#markup' => '<div class="messages warning">' . $no_fields_found . $usage_instructions . '</div>',
        '#type' => 'item',
      );
    }
    else {
      $form['fields'] = array(
        '#type' => 'checkboxes',
        '#title' => t('Fields'),
        '#options' => $field_options,
        '#description' => t('Select which fields from the referenced entity you want to load on changing the value of this field.'),
        '#default_value' => isset($settings['fields']) ? $settings['fields'] : array(),
      );
    }
    return $form;
  }

  /**
   * Only show handler on supported widget types.
   */
  public function access($field, $instance) {
    $is_single_value = $field['cardinality'] == 1;
    $is_supported = $is_single_value && array_key_exists($instance['widget']['type'], _entityreference_autofill_supported_widgets());
    return $is_supported;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityReferenceAutofillInstanceBehavior::access public function Only show handler on supported widget types. Overrides EntityReference_BehaviorHandler_Abstract::access
EntityReferenceAutofillInstanceBehavior::settingsForm public function Generate a settings form for this handler. Overrides EntityReference_BehaviorHandler_Abstract::settingsForm
EntityReference_BehaviorHandler_Abstract::$behavior protected property The name of the behavior plugin.
EntityReference_BehaviorHandler_Abstract::$plugin protected property The plugin definition.
EntityReference_BehaviorHandler_Abstract::delete public function Act before deleting an entity with an entity reference field. Overrides EntityReference_BehaviorHandler::delete 2
EntityReference_BehaviorHandler_Abstract::entityPostDelete public function Act after deleting an entity. Overrides EntityReference_BehaviorHandler::entityPostDelete
EntityReference_BehaviorHandler_Abstract::entityPostInsert public function Act after inserting an entity. Overrides EntityReference_BehaviorHandler::entityPostInsert 1
EntityReference_BehaviorHandler_Abstract::entityPostUpdate public function Act after updating an entity. Overrides EntityReference_BehaviorHandler::entityPostUpdate 1
EntityReference_BehaviorHandler_Abstract::insert public function Act before inserting an entity reference field. Overrides EntityReference_BehaviorHandler::insert 2
EntityReference_BehaviorHandler_Abstract::is_empty_alter public function Alter the empty status of a field item. Overrides EntityReference_BehaviorHandler::is_empty_alter
EntityReference_BehaviorHandler_Abstract::load public function Act on loading entity reference fields of entities. Overrides EntityReference_BehaviorHandler::load 2
EntityReference_BehaviorHandler_Abstract::postDelete public function Act after deleting an entity with an entity reference field. Overrides EntityReference_BehaviorHandler::postDelete
EntityReference_BehaviorHandler_Abstract::postInsert public function Act after inserting an entity reference field. Overrides EntityReference_BehaviorHandler::postInsert
EntityReference_BehaviorHandler_Abstract::postUpdate public function Act after updating an entity reference field. Overrides EntityReference_BehaviorHandler::postUpdate
EntityReference_BehaviorHandler_Abstract::presave public function Act on presaving an entity reference field. Overrides EntityReference_BehaviorHandler::presave
EntityReference_BehaviorHandler_Abstract::property_info_alter public function Alter the properties information of a field instance. Overrides EntityReference_BehaviorHandler::property_info_alter
EntityReference_BehaviorHandler_Abstract::schema_alter public function Alter the field schema. Overrides EntityReference_BehaviorHandler::schema_alter
EntityReference_BehaviorHandler_Abstract::update public function Act before updating an entity reference field. Overrides EntityReference_BehaviorHandler::update 2
EntityReference_BehaviorHandler_Abstract::validate public function Act on validating an entity reference field. Overrides EntityReference_BehaviorHandler::validate
EntityReference_BehaviorHandler_Abstract::views_data_alter public function Alter the views data of a field. Overrides EntityReference_BehaviorHandler::views_data_alter 1
EntityReference_BehaviorHandler_Abstract::__construct public function Constructor for the behavior. Overrides EntityReference_BehaviorHandler::__construct