You are here

function imagefield_focus_widget_image_image_process in ImageField Focus 7

Element #process callback function; process widget type image_image.

File

./imagefield_focus.module, line 135

Code

function imagefield_focus_widget_image_image_process($element, &$form_state, $form) {
  static $added;
  $item = $element['#value'];
  $instance = field_widget_instance($element, $form_state);
  $settings = $instance['settings'];
  $key = implode('-', array(
    $instance['entity_type'],
    $instance['bundle'],
    $instance['field_name'],
  ));

  // Add JS and CSS.
  $path = drupal_get_path('module', 'imagefield_focus');
  drupal_add_js($path . '/imgfocus/jquery.imgfocus.min.js');
  drupal_add_js($path . '/imagefield_focus.js');
  drupal_add_css($path . '/imgfocus/jquery.imgfocus.css');
  drupal_add_css($path . '/imagefield_focus.css');

  // Add settings.
  if (!isset($added[$key])) {
    list($w, $h) = explode('x', $settings['focus_min_size']) + array(
      '',
      '',
    );
    drupal_add_js(array(
      'imagefield_focus' => array(
        $key => array(
          'min_width' => intval($w),
          'min_height' => intval($h),
          'lock_ratio' => !empty($settings['focus_lock_ratio']),
        ),
      ),
    ), 'setting');
    $added[$key] = TRUE;
  }

  // Add focus fields.
  $element['focus_rect'] = array(
    '#type' => 'textfield',
    '#title' => t('Focus rectangle'),
    '#default_value' => isset($item['focus_rect']) ? $item['focus_rect'] : '',
    '#description' => t('The important portion of the image to set focus to and that should <strong>never</strong> be cut. It is recommended to keep it as small as possible for best results.'),
    '#attributes' => array(
      'class' => array(
        'imagefield-focus',
        'focus-rect',
      ),
      'data-settings-key' => $key,
    ),
    '#access' => (bool) $item['fid'],
  );
  $element['crop_rect'] = array(
    '#type' => 'textfield',
    '#title' => t('Crop rectangle'),
    '#default_value' => isset($item['crop_rect']) ? $item['crop_rect'] : '',
    '#description' => t('When set, the portions of the image outside this area will <strong>always</strong> be cut out.'),
    '#attributes' => array(
      'class' => array(
        'imagefield-focus',
        'crop-rect',
      ),
      'data-settings-key' => $key,
    ),
    '#access' => (bool) $item['fid'],
  );

  // Add focus box.
  if ($element['#file']) {
    $uri = $element['#file']->uri;
    $info = isset($element['#value']['width']) && isset($element['#value']['height']) ? array(
      'width' => $element['#value']['width'],
      'height' => $element['#value']['height'],
    ) : image_get_info($uri);
    if (is_array($info)) {
      $element['focus_box'] = array(
        '#markup' => '<div class="imagefield-focus focus-box"><img src="' . file_create_url($uri) . '" alt="' . $info['width'] . 'x' . $info['height'] . '" style="display:none;" /></div>',
      );
    }
  }
  $element['#element_validate'][] = 'imagefield_focus_widget_validate';
  return $element;
}