You are here

function angular_media_field_widget_form in CKEditor Widgets 7

Implements hook_field_widget_form().

File

includes/angular_media.field.inc, line 99

Code

function angular_media_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  $instance['fieldName'] = $instance['field_name'];
  $settings = $instance['widget']['settings'];
  $options = array(
    'style' => $settings['style'],
    'cardinality' => $field['cardinality'],
    'fieldName' => $instance['fieldName'],
    'files' => array(),
    'cropRatio' => $settings['cropRatio'],
    'flickrKey' => variable_get('angular_media_flickrKey', FALSE),
    'flickrSearch' => variable_get('angular_media_flickrSey', FALSE),
  );
  if (!empty($_POST[$instance['fieldName'] . '_media'])) {
    $options['files'] = json_decode($_POST[$instance['fieldName'] . '_media']);
  }
  $options = array_merge($settings, $options);
  foreach ($items as $item) {
    $options['files'][] = angular_media_simplify_file($item);
  }

  //dpm($options);
  $path = angular_media_app_path();
  $form['#attached'] = array(
    // @todo: make libraries_load work

    //'libraries_load' => array(

    //  array('angular_media', 'angular-media-app'),

    //)
    'js' => array(
      $path . 'js/libraries.min.js' => array(),
      $path . 'js/app.min.js' => array(),
      //$path . 'js/vendor/jcrop.min.js' => array('weight' => 10),
      drupal_get_path('module', 'angular_media') . '/js/angular-media.js' => array(),
    ),
    'css' => array(
      $path . 'css/app.min.css' => array(),
    ),
  );
  $element += array(
    '#type' => 'textfield',
    '#title' => $instance['label'],
    '#required' => $instance['required'],
    '#suffix' => theme('angular_media_app', array(
      'settings' => $options,
    )),
    '#attributes' => array(
      'style' => 'display:none',
    ),
  );
  if ($settings['style'] == 'placeholder') {
    $element['#title'] = NULL;
  }

  //@todo: this is hacky
  $form['#validate'][] = 'angular_media_field_widget_form_validate';
  $form['#submit'][] = 'angular_media_field_widget_form_validate';
  $form['angular_media_fields'] = isset($form['angular_media_fields']) ? $form['angular_media_fields'] : array(
    '#type' => 'value',
    '#value' => array(),
  );
  $form['angular_media_fields']['#value'][] = $instance['fieldName'];
  return array(
    $element,
  );
}