You are here

function mvf_unit_suggester_per_user_enabled in Measured Value Field 7

Notification for unit suggester that it was enabled in some formatter.

Whenever some unit suggester is enabled in any instance/formatter this function is invoked in the corresponding unit suggester plugin in order to allow the unit suggester to react and do some necessary initialization tasks.

Parameters

object $measure: Fully loaded 'units_measure' entity, on which is set up the MVF field

array $field: Field API field definition array of MVF field

array $instance: Field API instance definition array of MVF field

string $view_mode: View mode (key in $instance['display'] array) in which your unit suggester was enabled. It may be NULL, if your unit suggester is enabled on the instance level

array $settings: Array of currently stored settings in the provided $instance

array $plugin: cTools plugin definition array of your unit suggester plugin

1 string reference to 'mvf_unit_suggester_per_user_enabled'
per_user.inc in mvf_per_user/plugins/unit_suggesters/per_user.inc

File

mvf_per_user/plugins/unit_suggesters/per_user.inc, line 40

Code

function mvf_unit_suggester_per_user_enabled($measure, $field, $instance, $view_mode, $settings = array(), $plugin) {

  // Making sure corresponding unit field is created.
  if (!mvf_per_user_unit_field_load($field)) {
    try {
      $unit_field = array(
        'type' => $field['settings']['meta_info']['unit']['field_type'],
        'field_name' => mvf_per_user_unit_field_name($field),
        // We allow to edit this field via Field UI because folks may want to
        // customize lots of things about it.
        'locked' => FALSE,
        // We will copy settings for our entityreference field from
        // corresponding part of the MVF field.
        'settings' => $field['settings']['unit'],
      );
      $unit_field = field_create_field($unit_field);
      $unit_instance = array(
        'field_name' => $unit_field['field_name'],
        'entity_type' => 'user',
        'bundle' => 'user',
        'label' => t('Output Unit for @instance', array(
          '@instance' => $instance['label'],
        )),
        'description' => t('Specify here in what units you want to see %instance field.', array(
          '%instance' => $instance['label'],
        )),
        'widget' => array(
          'type' => $field['settings']['meta_info']['unit']['widget'],
        ),
      );
      field_create_instance($unit_instance);
    } catch (FieldException $e) {
      drupal_set_message($e
        ->getMessage(), 'error');
    }
  }
}