You are here

function easychart_field_widget_form in Easychart 7

Same name and namespace in other branches
  1. 7.3 easychart.module \easychart_field_widget_form()
  2. 7.2 easychart.module \easychart_field_widget_form()

Implements hook_field_widget_form().

File

./easychart.module, line 137
Easy Chart module file.

Code

function easychart_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  switch ($instance['widget']['type']) {
    case 'easychart':

      // We keep this global to prevent issues with multiple widgets on the same page.
      static $settings;

      // Provide a wrapper and add the required js and css.
      if (empty($settings)) {
        $settings = array(
          'unwantedOptions' => variable_get('unwanted_options', 'global, lang, exporting, series, labels, navigation, loading, pane, plotOptions, xAxis-plotLines'),
          'unwantedReturnTypes' => variable_get('unwanted_return_types', 'Function, CSSObject, null'),
          'optionsStep1' => variable_get('options_step1', 'chart--type'),
          'optionsStep2' => variable_get('options_step2', 'title--text, chart--backgroundColor, subtitle--text, yAxis-title--text'),
          'defaultColors' => variable_get('default_colors', ''),
        );
        drupal_add_js(array(
          'easychart' => $settings,
        ), 'setting');
      }
      $element = array(
        '#prefix' => '<div class="easychart-wrapper clearfix">',
        '#suffix' => '</div>',
        '#attached' => array(
          // Add highcharts plugin.
          'libraries_load' => array(
            array(
              'highcharts',
            ),
            array(
              'easychart',
            ),
          ),
          'js' => array(
            drupal_get_path('module', 'easychart') . '/js/easychart.js',
          ),
          'css' => array(
            drupal_get_path('module', 'easychart') . '/css/easychart.admin.css',
          ),
        ),
      );
      $element['config'] = array(
        '#description' => t('The configuration options as described at http://api.highcharts.com/highcharts'),
        '#type' => 'hidden',
        '#default_value' => isset($items[$delta]['config']) ? $items[$delta]['config'] : NULL,
        '#attributes' => array(
          'class' => array(
            'easychart-config',
          ),
        ),
      );
      $element['stored'] = array(
        '#type' => 'hidden',
        '#default_value' => isset($items[$delta]['stored']) ? $items[$delta]['stored'] : NULL,
        '#attributes' => array(
          'class' => array(
            'easychart-stored',
          ),
        ),
      );
      $element['csv'] = array(
        '#description' => t('Your chart data in CSV format'),
        '#type' => 'hidden',
        '#default_value' => isset($items[$delta]['csv']) ? $items[$delta]['csv'] : NULL,
        '#attributes' => array(
          'class' => array(
            'easychart-csv',
          ),
        ),
      );
      $element['csv_url'] = array(
        '#description' => t('The URL to a CSV file'),
        '#type' => 'hidden',
        '#default_value' => isset($items[$delta]['csv_url']) ? $items[$delta]['csv_url'] : NULL,
        '#attributes' => array(
          'class' => array(
            'easychart-csv-url',
          ),
        ),
      );

      // Edit/add/configure links for the popup.
      $delete_link = l(t('Delete chart'), '#', array(
        'attributes' => array(
          'class' => array(
            'easychart-delete-link',
            'element-hidden',
          ),
        ),
      ));
      $configure_link = l(t('Add chart'), '#', array(
        'attributes' => array(
          'class' => array(
            'button',
            'easychart-configure-link',
          ),
        ),
      ));
      $popup = '<div class="easychart-popup"><div class="easychart-bar"><a href="#" class="close">Save and close popup</a> <a href="#" class="cancel">Cancel</a></div><div class="easychart-popup-content"></div></div>';
      $element['link'] = array(
        '#markup' => $configure_link . $delete_link . $popup,
        '#prefix' => '<div class="easychart-left">',
        '#suffix' => '</div>',
      );

      // Preview placeholder. Actual preview is rendered in easychart.js.
      $element['preview'] = array(
        '#title' => t('Chart preview'),
        '#markup' => '',
        '#prefix' => '<div class="easychart-preview">',
        '#suffix' => '</div>',
      );
      break;
  }
  return $element;
}