cms/drupal/modules/field_ui/field_ui.api.php
changeset 541 e756a8c72c3d
equal deleted inserted replaced
540:07239de796bb 541:e756a8c72c3d
       
     1 <?php
       
     2 
       
     3 /**
       
     4  * @file
       
     5  * Hooks provided by the Field UI module.
       
     6  */
       
     7 
       
     8 /**
       
     9  * @addtogroup field_types
       
    10  * @{
       
    11  */
       
    12 
       
    13 /**
       
    14  * Add settings to a field settings form.
       
    15  *
       
    16  * Invoked from field_ui_field_settings_form() to allow the module defining the
       
    17  * field to add global settings (i.e. settings that do not depend on the bundle
       
    18  * or instance) to the field settings form. If the field already has data, only
       
    19  * include settings that are safe to change.
       
    20  *
       
    21  * @todo: Only the field type module knows which settings will affect the
       
    22  * field's schema, but only the field storage module knows what schema
       
    23  * changes are permitted once a field already has data. Probably we need an
       
    24  * easy way for a field type module to ask whether an update to a new schema
       
    25  * will be allowed without having to build up a fake $prior_field structure
       
    26  * for hook_field_update_forbid().
       
    27  *
       
    28  * @param $field
       
    29  *   The field structure being configured.
       
    30  * @param $instance
       
    31  *   The instance structure being configured.
       
    32  * @param $has_data
       
    33  *   TRUE if the field already has data, FALSE if not.
       
    34  *
       
    35  * @return
       
    36  *   The form definition for the field settings.
       
    37  */
       
    38 function hook_field_settings_form($field, $instance, $has_data) {
       
    39   $settings = $field['settings'];
       
    40   $form['max_length'] = array(
       
    41     '#type' => 'textfield',
       
    42     '#title' => t('Maximum length'),
       
    43     '#default_value' => $settings['max_length'],
       
    44     '#required' => FALSE,
       
    45     '#element_validate' => array('element_validate_integer_positive'),
       
    46     '#description' => t('The maximum length of the field in characters. Leave blank for an unlimited size.'),
       
    47   );
       
    48   return $form;
       
    49 }
       
    50 
       
    51 /**
       
    52  * Add settings to an instance field settings form.
       
    53  *
       
    54  * Invoked from field_ui_field_edit_form() to allow the module defining the
       
    55  * field to add settings for a field instance.
       
    56  *
       
    57  * @param $field
       
    58  *   The field structure being configured.
       
    59  * @param $instance
       
    60  *   The instance structure being configured.
       
    61  *
       
    62  * @return
       
    63  *   The form definition for the field instance settings.
       
    64  */
       
    65 function hook_field_instance_settings_form($field, $instance) {
       
    66   $settings = $instance['settings'];
       
    67 
       
    68   $form['text_processing'] = array(
       
    69     '#type' => 'radios',
       
    70     '#title' => t('Text processing'),
       
    71     '#default_value' => $settings['text_processing'],
       
    72     '#options' => array(
       
    73       t('Plain text'),
       
    74       t('Filtered text (user selects text format)'),
       
    75     ),
       
    76   );
       
    77   if ($field['type'] == 'text_with_summary') {
       
    78     $form['display_summary'] = array(
       
    79       '#type' => 'select',
       
    80       '#title' => t('Display summary'),
       
    81       '#options' => array(
       
    82         t('No'),
       
    83         t('Yes'),
       
    84       ),
       
    85       '#description' => t('Display the summary to allow the user to input a summary value. Hide the summary to automatically fill it with a trimmed portion from the main post.'),
       
    86       '#default_value' => !empty($settings['display_summary']) ? $settings['display_summary'] :  0,
       
    87     );
       
    88   }
       
    89 
       
    90   return $form;
       
    91 }
       
    92 
       
    93 /**
       
    94  * Add settings to a widget settings form.
       
    95  *
       
    96  * Invoked from field_ui_field_edit_form() to allow the module defining the
       
    97  * widget to add settings for a widget instance.
       
    98  *
       
    99  * @param $field
       
   100  *   The field structure being configured.
       
   101  * @param $instance
       
   102  *   The instance structure being configured.
       
   103  *
       
   104  * @return
       
   105  *   The form definition for the widget settings.
       
   106  */
       
   107 function hook_field_widget_settings_form($field, $instance) {
       
   108   $widget = $instance['widget'];
       
   109   $settings = $widget['settings'];
       
   110 
       
   111   if ($widget['type'] == 'text_textfield') {
       
   112     $form['size'] = array(
       
   113       '#type' => 'textfield',
       
   114       '#title' => t('Size of textfield'),
       
   115       '#default_value' => $settings['size'],
       
   116       '#element_validate' => array('element_validate_integer_positive'),
       
   117       '#required' => TRUE,
       
   118     );
       
   119   }
       
   120   else {
       
   121     $form['rows'] = array(
       
   122       '#type' => 'textfield',
       
   123       '#title' => t('Rows'),
       
   124       '#default_value' => $settings['rows'],
       
   125       '#element_validate' => array('element_validate_integer_positive'),
       
   126       '#required' => TRUE,
       
   127     );
       
   128   }
       
   129 
       
   130   return $form;
       
   131 }
       
   132 
       
   133 
       
   134 /**
       
   135  * Specify the form elements for a formatter's settings.
       
   136  *
       
   137  * This hook is only invoked if hook_field_formatter_settings_summary()
       
   138  * returns a non-empty value.
       
   139  *
       
   140  * @param $field
       
   141  *   The field structure being configured.
       
   142  * @param $instance
       
   143  *   The instance structure being configured.
       
   144  * @param $view_mode
       
   145  *   The view mode being configured.
       
   146  * @param $form
       
   147  *   The (entire) configuration form array, which will usually have no use here.
       
   148  * @param $form_state
       
   149  *   The form state of the (entire) configuration form.
       
   150  *
       
   151  * @return
       
   152  *   The form elements for the formatter settings.
       
   153  */
       
   154 function hook_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
       
   155   $display = $instance['display'][$view_mode];
       
   156   $settings = $display['settings'];
       
   157 
       
   158   $element = array();
       
   159 
       
   160   if ($display['type'] == 'text_trimmed' || $display['type'] == 'text_summary_or_trimmed') {
       
   161     $element['trim_length'] = array(
       
   162       '#title' => t('Length'),
       
   163       '#type' => 'textfield',
       
   164       '#size' => 20,
       
   165       '#default_value' => $settings['trim_length'],
       
   166       '#element_validate' => array('element_validate_integer_positive'),
       
   167       '#required' => TRUE,
       
   168     );
       
   169   }
       
   170 
       
   171   return $element;
       
   172 
       
   173 }
       
   174 
       
   175 /**
       
   176  * Return a short summary for the current formatter settings of an instance.
       
   177  *
       
   178  * If an empty result is returned, the formatter is assumed to have no
       
   179  * configurable settings, and no UI will be provided to display a settings
       
   180  * form.
       
   181  *
       
   182  * @param $field
       
   183  *   The field structure.
       
   184  * @param $instance
       
   185  *   The instance structure.
       
   186  * @param $view_mode
       
   187  *   The view mode for which a settings summary is requested.
       
   188  *
       
   189  * @return
       
   190  *   A string containing a short summary of the formatter settings.
       
   191  */
       
   192 function hook_field_formatter_settings_summary($field, $instance, $view_mode) {
       
   193   $display = $instance['display'][$view_mode];
       
   194   $settings = $display['settings'];
       
   195 
       
   196   $summary = '';
       
   197 
       
   198   if ($display['type'] == 'text_trimmed' || $display['type'] == 'text_summary_or_trimmed') {
       
   199     $summary = t('Length: @chars chars', array('@chars' => $settings['trim_length']));
       
   200   }
       
   201 
       
   202   return $summary;
       
   203 }
       
   204 
       
   205 /**
       
   206  * @} End of "addtogroup field_types".
       
   207  */