|
@@ -1,45 +1,8 @@
|
|
|
<?php
|
|
|
|
|
|
-
|
|
|
-/**
|
|
|
- * A base for fields attached Tripal Entities.
|
|
|
- *
|
|
|
- * This class is intended to simplify development of fields for Tripal Entities.
|
|
|
- * The Drupal Field API can still be used if desired, but the hope for this
|
|
|
- * class it to put the necessary functions in one place so that other Tripal
|
|
|
- * developers do not need to suffer the pain of navigating and learning the
|
|
|
- * Drupal Field API.
|
|
|
- *
|
|
|
- * To create a new field that can be attached to a Tripal Entity follow these
|
|
|
- * steps:
|
|
|
- * # Create a new class that inherits from TripalField
|
|
|
- * # Copy the editable constant variables (default_desription, default_label,
|
|
|
- * default_settings and default_storage) to your class and edit as needed.
|
|
|
- * Be sure not to rename these variables and be sure to keep the 'static'
|
|
|
- * qualifier on them.
|
|
|
- * # Copy the functions you want to override. You will not need to copy the
|
|
|
- * constructor, the static info() functions, or the getters and setters. In
|
|
|
- * short you'll typically only need to override the Settingsform funtions,
|
|
|
- * their validators and submitters, the load() function, and the widgetForm
|
|
|
- * with it's validator and submitter.
|
|
|
- * # In your custom module implement the function hook_create_tripalfields()
|
|
|
- * This function will be called anytime a new TripalEntity is created. It
|
|
|
- * allows your module to create the new fields. See the documentation for
|
|
|
- * this function for creating the fields. A field is usually only ever
|
|
|
- * created once and can be reused on multiple entities. So, even though
|
|
|
- * this function is called everytime a new TripalEntity is created the fields
|
|
|
- * will only be created once.
|
|
|
- * # In your custom module implement the function
|
|
|
- * hook_create_tripalfield_instance(). This function is called anytime a
|
|
|
- * new TripalEntity is created. It allows you to specify which fields are
|
|
|
- * attached to an entity. See the documentation for this hook function for
|
|
|
- * more information.
|
|
|
- *
|
|
|
- */
|
|
|
class TripalField {
|
|
|
|
|
|
|
|
|
-
|
|
|
// --------------------------------------------------------------------------
|
|
|
// EDITABLE STATIC CONSTANTS
|
|
|
//
|
|
@@ -48,27 +11,40 @@ class TripalField {
|
|
|
// the field and it's default widget and formatter.
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
+ // The term that this field maps to. The format for the term should be:
|
|
|
+ // [vocab]:[accession] where [vocab] is the short name of the vocabulary
|
|
|
+ // and [acession] is the unique accession number for the term. This term
|
|
|
+ // must already exist in the vocabulary storage backend. This
|
|
|
+ // value should never be changed once fields exist for this type.
|
|
|
+ public static $term = 'schema:Thing';
|
|
|
+
|
|
|
// The default lable for this field.
|
|
|
- public static $default_label = 'Tripal Field.';
|
|
|
+ public static $label = 'Tripal Field.';
|
|
|
|
|
|
// The default description for this field.
|
|
|
- public static $default_description = 'The generic base class for all
|
|
|
- Tripal Fields. Replace this text as appropriate for the child implementation.';
|
|
|
+ public static $description = 'The generic base class for all Tripal fields. ' .
|
|
|
+ 'Replace this text as appropriate for the child implementation.';
|
|
|
|
|
|
// Provide a list of global settings. These can be accessed witihn the
|
|
|
// globalSettingsForm. When the globalSettingsForm is submitted then
|
|
|
// Drupal will automatically change these settings for all fields.
|
|
|
- public static $default_settings = array();
|
|
|
+ public static $settings = array();
|
|
|
|
|
|
// Provide a list of instance specific settings. These can be access within
|
|
|
// the instanceSettingsForm. When the instanceSettingsForm is submitted
|
|
|
// then Drupal with automatically change these settings for the instnace.
|
|
|
// It is recommended to put settings at the instance level whenever possible.
|
|
|
- public static $default_instance_settings = array();
|
|
|
+ public static $instance_settings = array();
|
|
|
|
|
|
// Set this to the name of the storage backend that by default will support
|
|
|
// this field.
|
|
|
- public static $default_storage = 'tripal_no_storage';
|
|
|
+ public static $storage = 'tripal_no_storage';
|
|
|
+
|
|
|
+ // The default widget for this field.
|
|
|
+ public static $default_widget = '';
|
|
|
+
|
|
|
+ // The default formatter for this field.
|
|
|
+ public static $default_formatter = '';
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
// PROTECTED CLASS MEMBERS -- DO NOT OVERRIDE
|
|
@@ -99,6 +75,15 @@ class TripalField {
|
|
|
public function __construct($field, $instance = NULL) {
|
|
|
$this->field = $field;
|
|
|
$this->instance = $instance;
|
|
|
+
|
|
|
+ $class = get_called_class();
|
|
|
+ // Make sure the term exist.
|
|
|
+ list($vocabulary, $accession) = preg_split('/:/', $class::$term);
|
|
|
+ $term = tripal_get_term_details($vocabulary, $accession);
|
|
|
+ if (!$term) {
|
|
|
+ //throw new Error(t('Cannot create TripalField of type "%term" as that
|
|
|
+ // term does not exist.', array('%term' => $class::$term)));
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
@@ -116,72 +101,23 @@ class TripalField {
|
|
|
* describing the field type. The keys are the same as for the
|
|
|
* hook_field_info() function.
|
|
|
*/
|
|
|
- public static function globalInfo() {
|
|
|
- $field_type = get_called_class();
|
|
|
+ public static function info() {
|
|
|
+ $class = get_called_class();
|
|
|
return array(
|
|
|
- 'label' => $field_type::$default_label,
|
|
|
- 'description' => $field_type::$default_description,
|
|
|
- 'default_widget' => $field_type . '_widget',
|
|
|
- 'default_formatter' => $field_type . '_formatter',
|
|
|
- 'settings' => $field_type::$default_settings,
|
|
|
- 'instance_settings' => $field_type::$default_instance_settings,
|
|
|
+ 'label' => $class::$label,
|
|
|
+ 'description' => $class::$description,
|
|
|
+ 'default_widget' => $class::$default_widget,
|
|
|
+ 'default_formatter' => $class::$default_formatter,
|
|
|
+ 'settings' => $class::$settings,
|
|
|
+ 'instance_settings' => $class::$instance_settings,
|
|
|
'storage' => array(
|
|
|
- 'type' => $field_type::$default_storage,
|
|
|
+ 'type' => $class::$storage,
|
|
|
'module' => 'tripal',
|
|
|
'active' => TRUE
|
|
|
),
|
|
|
);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * Provides information about the widgets provided by this field.
|
|
|
- *
|
|
|
- * This function corresponds to the hook_field_widget_info() function of
|
|
|
- * the Drupal Field API.
|
|
|
- *
|
|
|
- * This is a static function as it provides default values for all of the
|
|
|
- * widgets for this field type, and thus we don't need an instantiated
|
|
|
- * object to provide this information.
|
|
|
- *
|
|
|
- * @return
|
|
|
- * An associative array with key/value pairs compatible with those from the
|
|
|
- * hook_field_widget_info() function of the Drupal Field API.
|
|
|
- */
|
|
|
- public static function widgetInfo() {
|
|
|
- $field_type = get_called_class();
|
|
|
- return array(
|
|
|
- $field_type . '_widget' => array(
|
|
|
- 'label' => $field_type::$default_label,
|
|
|
- 'field types' => array($field_type)
|
|
|
- ),
|
|
|
- );
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Provides information about the formatter for this field.
|
|
|
- *
|
|
|
- * This function corresponds to the hook_field_formatter_info() function of
|
|
|
- * the Drupal Field API.
|
|
|
- *
|
|
|
- * This is a static function as it provides default values for all of the
|
|
|
- * formatters for this field type, and thus we don't need an instantiated
|
|
|
- * object to provide this information.
|
|
|
- *
|
|
|
- * @return
|
|
|
- * An associative array with key/value paris compatible with those from the
|
|
|
- * hook_field_formatter_info() function of the Drupal Field API.
|
|
|
- *
|
|
|
- */
|
|
|
- public static function formatterInfo() {
|
|
|
- $field_type = get_called_class();
|
|
|
- return array(
|
|
|
- $field_type . '_formatter' => array(
|
|
|
- 'label' => $field_type::$default_label,
|
|
|
- 'field types' => array($field_type),
|
|
|
- 'settings' => array(),
|
|
|
- ),
|
|
|
- );
|
|
|
- }
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
// GETTERS AND SETTERS -- DO NOT OVERRIDE
|
|
@@ -205,185 +141,6 @@ class TripalField {
|
|
|
}
|
|
|
|
|
|
|
|
|
- // --------------------------------------------------------------------------
|
|
|
- // OVERRIDEABLE FUNCTIONS
|
|
|
- // --------------------------------------------------------------------------
|
|
|
-
|
|
|
- /**
|
|
|
- * Provides a summary of the formatter settings.
|
|
|
- *
|
|
|
- * This function corresponds to the hook_field_formatter_settings_summary()
|
|
|
- * function of the Drupal Field API.
|
|
|
- *
|
|
|
- * On the 'Manage Display' page of the content type administration page,
|
|
|
- * fields are allowed to provide a settings form. This settings form can
|
|
|
- * be used to allow the site admin to define how the field should be
|
|
|
- * formatted. The settings are then available for the formatter()
|
|
|
- * function of this class. This function provides a text-based description
|
|
|
- * of the settings for the site developer to see. It appears on the manage
|
|
|
- * display page inline with the field. A field must always return a
|
|
|
- * value in this function if the settings form gear button is to appear.
|
|
|
- *
|
|
|
- * See the hook_field_formatter_settings_summary() function for more
|
|
|
- * information.
|
|
|
- *
|
|
|
- * @param $field
|
|
|
- * @param $instance
|
|
|
- * @param $view_mode
|
|
|
- *
|
|
|
- * @return string
|
|
|
- * A string that provides a very brief summary of the field settings
|
|
|
- * to the user.
|
|
|
- *
|
|
|
- */
|
|
|
- public function formatterSettingsSummary($view_mode) {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Provides the field's setting form.
|
|
|
- *
|
|
|
- * This function corresponds to the hook_field_formatter_settings_form()
|
|
|
- * function of the Drupal Field API.
|
|
|
- *
|
|
|
- * The settings form appears on the 'Manage Display' page of the content
|
|
|
- * type administration page. This function provides the form that will
|
|
|
- * appear on that page.
|
|
|
- *
|
|
|
- * To add a validate function, please create a static function in the
|
|
|
- * implementing class, and indicate that this function should be used
|
|
|
- * in the form array that is returned by this function.
|
|
|
- *
|
|
|
- * This form will not be displayed if the formatter_settings_summary()
|
|
|
- * function does not return anything.
|
|
|
- *
|
|
|
- * @param $field
|
|
|
- * @param $instance
|
|
|
- * @param $view_mode
|
|
|
- * @param $form
|
|
|
- * @param $form_state
|
|
|
- *
|
|
|
- * @return
|
|
|
- * A Drupal Form array containing the settings form for this field.
|
|
|
- */
|
|
|
- public function formatterSettingsForm($view_mode, $form, &$form_state) {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Provides the display for a field
|
|
|
- *
|
|
|
- * This function corresponds to the hook_field_formatter_view()
|
|
|
- * function of the Drupal Field API.
|
|
|
- *
|
|
|
- * This function provides the display for a field when it is viewed on
|
|
|
- * the web page. The content returned by the formatter should only include
|
|
|
- * what is present in the $items[$delta]['values] array. This way, the
|
|
|
- * contents that are displayed on the page, via webservices and downloaded
|
|
|
- * into a CSV file will always be identical. The view need not show all
|
|
|
- * of the data in the 'values' array.
|
|
|
- *
|
|
|
- * @param $element
|
|
|
- * @param $entity_type
|
|
|
- * @param $entity
|
|
|
- * @param $langcode
|
|
|
- * @param $items
|
|
|
- * @param $display
|
|
|
- *
|
|
|
- * @return
|
|
|
- * An element array compatible with that returned by the
|
|
|
- * hook_field_formatter_view() function.
|
|
|
- */
|
|
|
- public function formatterView(&$element, $entity_type, $entity, $langcode, $items, $display) {
|
|
|
-
|
|
|
- foreach($items as $delta => $item) {
|
|
|
- $element[$delta] = array(
|
|
|
- '#type' => 'markup',
|
|
|
- '#markup' => $item['value'],
|
|
|
- );
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Provides the form for editing of this field.
|
|
|
- *
|
|
|
- * This function corresponds to the hook_field_widget_form()
|
|
|
- * function of the Drupal Field API.
|
|
|
- *
|
|
|
- * This form is diplayed when the user creates a new entity or edits an
|
|
|
- * existing entity. If the field is attached to the entity then the form
|
|
|
- * provided by this function will be displayed.
|
|
|
- *
|
|
|
- * At a minimum, the form must have a 'value' element. For Tripal, the
|
|
|
- * 'value' element of a field always corresponds to the value that is
|
|
|
- * presented to the end-user either directly on the page (with formatting)
|
|
|
- * or via web services, or some other mechanism. However, the 'value' is
|
|
|
- * sometimes not enough for a field. For example, the Tripal Chado module
|
|
|
- * maps fields to table columns and sometimes those columns are foreign keys
|
|
|
- * therefore, the Tripal Chado modules does not just use the 'value' but adds
|
|
|
- * additional elements to help link records via FKs. But even in this case
|
|
|
- * the 'value' element must always be present in the return form and in such
|
|
|
- * cases it's value should be set equal to that added in the 'load' function.
|
|
|
- *
|
|
|
- * @param $widget
|
|
|
- * @param $form
|
|
|
- * The form structure where widgets are being attached to. This might be a
|
|
|
- * full form structure, or a sub-element of a larger form.
|
|
|
- * @param $form_state
|
|
|
- * An associative array containing the current state of the form.
|
|
|
- * @param $langcode
|
|
|
- * The language associated with $items.
|
|
|
- * @param $items
|
|
|
- * Array of default values for this field.
|
|
|
- * @param $delta
|
|
|
- * The order of this item in the array of subelements (0, 1, 2, etc).
|
|
|
- * @param $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.
|
|
|
- * - #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 $form_state['values'], and is
|
|
|
- * used to access processing information for the field through the
|
|
|
- * field_form_get_state() and field_form_set_state() functions.
|
|
|
- * - #columns: A list of field storage columns of the field.
|
|
|
- * - #title: The sanitized element label for the field instance, ready for
|
|
|
- * output.
|
|
|
- * - #description: The sanitized element description for the field instance,
|
|
|
- * ready for output.
|
|
|
- * - #required: A Boolean indicating whether the element value is required;
|
|
|
- * for required multiple value fields, only the first widget's values are
|
|
|
- * required.
|
|
|
- * - #delta: The order of this item in the array of subelements; see
|
|
|
- * $delta above
|
|
|
- */
|
|
|
- public function widgetForm(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
|
|
|
-
|
|
|
- $widget['value'] = array(
|
|
|
- '#type' => 'value',
|
|
|
- '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
|
|
|
- );
|
|
|
- $widget['#field'] = $this->field;
|
|
|
- $widget['#instance'] = $this->instance;
|
|
|
- $widget['#element_validate'] = array('tripal_field_widget_form_validate');
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Performs validation of the widgetForm.
|
|
|
- *
|
|
|
- * Use this validate to ensure that form values are entered correctly. Note
|
|
|
- * this is different from the validate() function which ensures that the
|
|
|
- * field data meets expectations.
|
|
|
- *
|
|
|
- * @param $form
|
|
|
- * @param $form_state
|
|
|
- */
|
|
|
- public function widgetFormValidate($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* Perform validation of the field regardless how it is updated.
|
|
|
*
|
|
@@ -417,45 +174,6 @@ class TripalField {
|
|
|
}
|
|
|
|
|
|
|
|
|
- /**
|
|
|
- * Performs extra commands when the entity form is submitted.
|
|
|
- *
|
|
|
- * Drupal typically does not provide a submit hook for fields. The
|
|
|
- * TripalField provides one to allow for behind-the-scenes actions to
|
|
|
- * occur. This function should never be used for updates, deletes or
|
|
|
- * inserts for the Chado table associated with the field. Rather, the
|
|
|
- * storage backend should be allowed to handle inserts, updates deletes.
|
|
|
- * However, it is permissible to perform inserts, updates or deletions within
|
|
|
- * Chado using this function. Those operations can be performed if needed but
|
|
|
- * on other tables not directly associated with the field.
|
|
|
- *
|
|
|
- * An example is the chado.feature_synonym table. The chado_linker__synonym
|
|
|
- * field allows the user to provide a brand new synonynm and it must add it
|
|
|
- * to the chado.synonym table prior to the record in the
|
|
|
- * chado.feature_synonym table. This insert occurs in the widgetFormSubmit
|
|
|
- * function.
|
|
|
- *
|
|
|
- * @param $entity_type
|
|
|
- * The type of $entity.
|
|
|
- * @param $entity
|
|
|
- * The entity for the operation.
|
|
|
- * @param $field
|
|
|
- * The field structure for the operation.
|
|
|
- * @param $instance
|
|
|
- * The instance structure for $field on $entity's bundle.
|
|
|
- * @param $langcode
|
|
|
- * The language associated with $items.
|
|
|
- * @param $items
|
|
|
- * $entity->{$field['field_name']}[$langcode], or an empty array if unset.
|
|
|
- * @param $form
|
|
|
- * The submitted form array.
|
|
|
- * @param $form_state.
|
|
|
- * The form state array.
|
|
|
- */
|
|
|
- public function widgetFormSubmit($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* Loads the field values from the underlying data store.
|
|
|
*
|
|
@@ -488,64 +206,6 @@ class TripalField {
|
|
|
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * Provides a form for the 'Field Settings' of an instance of this field.
|
|
|
- *
|
|
|
- * This function corresponds to the hook_field_instance_settings_form()
|
|
|
- * function of the Drupal Field API.
|
|
|
- *
|
|
|
- * Validation of the instance settings form is not supported by Drupal, but
|
|
|
- * the TripalField class does provide a mechanism for supporting validation.
|
|
|
- * To allow for validation of your setting form you must call the parent
|
|
|
- * in your child class:
|
|
|
- *
|
|
|
- * @code
|
|
|
- * $element = parent::instanceSettingsForm();
|
|
|
- * @endcode
|
|
|
- *
|
|
|
- * Please note, the form generated with this function does not easily
|
|
|
- * support AJAX calls in the same way that other Drupal forms do. If you
|
|
|
- * need to use AJAX you must manually alter the $form in your ajax call.
|
|
|
- * The typical way to handle updating the form via an AJAX call is to make
|
|
|
- * the changes in the form function itself but that doesn't work here.
|
|
|
- */
|
|
|
- public function instanceSettingsForm() {
|
|
|
- $settings = $this->instance['settings'];
|
|
|
- $element = array();
|
|
|
-
|
|
|
- // $element['semantic_web'] = array(
|
|
|
- // '#type' => 'textfield',
|
|
|
- // '#title' => 'Semantic Web',
|
|
|
- // '#description' => t('Each field must be associated with a term
|
|
|
- // from a controlled vocabulary. This allows computer programs to understand
|
|
|
- // the data provided on this site. Please be cautions changing these
|
|
|
- // values. Defaults are set by Tripal and sites that use the same
|
|
|
- // terms can exchange information.'),
|
|
|
- // '#collapsed' => TRUE,
|
|
|
- // '#collapsible' => TRUE,
|
|
|
- // '#tree' => TRUE,
|
|
|
- // );
|
|
|
- $element['#field'] = $this->field;
|
|
|
- $element['#instance'] = $this->instance;
|
|
|
- $element['#element_validate'][] = 'tripal_field_instance_settings_form_validate';
|
|
|
-
|
|
|
- return $element;
|
|
|
- }
|
|
|
- /**
|
|
|
- * Provides validation of the instance settings form.
|
|
|
- *
|
|
|
- * There is no equivalent function in the Drupal Field API. Validation
|
|
|
- * of instance settings forms in Drupal is not supported. However, the
|
|
|
- * TripalField provides this function to fill the gap. See the
|
|
|
- * documentation for the instanceSettingsForm() function for instructions
|
|
|
- * to support use of this function.
|
|
|
- *
|
|
|
- * @param $form
|
|
|
- * @param $form_state
|
|
|
- */
|
|
|
- public function instanceSettingsFormValidate($form, &$form_state) {
|
|
|
-
|
|
|
- }
|
|
|
|
|
|
/**
|
|
|
* Provides a form for the 'Field Settings' of the field management page.
|
|
@@ -560,7 +220,7 @@ class TripalField {
|
|
|
* @param $has_data
|
|
|
* TRUE if the field already has data, FALSE if not.
|
|
|
*/
|
|
|
- public function globalSettingsForm($has_data) {
|
|
|
+ public function settingsForm($has_data) {
|
|
|
$settings = $this->field['settings'];
|
|
|
$element = array();
|
|
|
|
|
@@ -587,7 +247,7 @@ class TripalField {
|
|
|
* @param unknown $form
|
|
|
* @param unknown $form_state
|
|
|
*/
|
|
|
- public function globalSettingsFormValidate($form, &$form_state) {
|
|
|
+ public function settingsFormValidate($form, &$form_state) {
|
|
|
|
|
|
}
|
|
|
|