image.field.inc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. <?php
  2. /**
  3. * @file
  4. * Implement an image field, based on the file module's file field.
  5. */
  6. /**
  7. * Implements hook_field_info().
  8. */
  9. function image_field_info() {
  10. return array(
  11. 'image' => array(
  12. 'label' => t('Image'),
  13. 'description' => t('This field stores the ID of an image file as an integer value.'),
  14. 'settings' => array(
  15. 'uri_scheme' => variable_get('file_default_scheme', 'public'),
  16. 'default_image' => 0,
  17. ),
  18. 'instance_settings' => array(
  19. 'file_extensions' => 'png gif jpg jpeg',
  20. 'file_directory' => '',
  21. 'max_filesize' => '',
  22. 'alt_field' => 0,
  23. 'title_field' => 0,
  24. 'max_resolution' => '',
  25. 'min_resolution' => '',
  26. 'default_image' => 0,
  27. ),
  28. 'default_widget' => 'image_image',
  29. 'default_formatter' => 'image',
  30. ),
  31. );
  32. }
  33. /**
  34. * Implements hook_field_settings_form().
  35. */
  36. function image_field_settings_form($field, $instance) {
  37. $defaults = field_info_field_settings($field['type']);
  38. $settings = array_merge($defaults, $field['settings']);
  39. $scheme_options = array();
  40. foreach (file_get_stream_wrappers(STREAM_WRAPPERS_WRITE_VISIBLE) as $scheme => $stream_wrapper) {
  41. $scheme_options[$scheme] = $stream_wrapper['name'];
  42. }
  43. $form['uri_scheme'] = array(
  44. '#type' => 'radios',
  45. '#title' => t('Upload destination'),
  46. '#options' => $scheme_options,
  47. '#default_value' => $settings['uri_scheme'],
  48. '#description' => t('Select where the final files should be stored. Private file storage has significantly more overhead than public files, but allows restricted access to files within this field.'),
  49. );
  50. // When the user sets the scheme on the UI, even for the first time, it's
  51. // updating a field because fields are created on the "Manage fields"
  52. // page. So image_field_update_field() can handle this change.
  53. $form['default_image'] = array(
  54. '#title' => t('Default image'),
  55. '#type' => 'managed_file',
  56. '#description' => t('If no image is uploaded, this image will be shown on display.'),
  57. '#default_value' => $field['settings']['default_image'],
  58. '#upload_location' => $settings['uri_scheme'] . '://default_images/',
  59. );
  60. return $form;
  61. }
  62. /**
  63. * Implements hook_field_instance_settings_form().
  64. */
  65. function image_field_instance_settings_form($field, $instance) {
  66. $settings = $instance['settings'];
  67. // Use the file field instance settings form as a basis.
  68. $form = file_field_instance_settings_form($field, $instance);
  69. // Add maximum and minimum resolution settings.
  70. $max_resolution = explode('x', $settings['max_resolution']) + array('', '');
  71. $form['max_resolution'] = array(
  72. '#type' => 'item',
  73. '#title' => t('Maximum image resolution'),
  74. '#element_validate' => array('_image_field_resolution_validate'),
  75. '#weight' => 4.1,
  76. '#field_prefix' => '<div class="container-inline">',
  77. '#field_suffix' => '</div>',
  78. '#description' => t('The maximum allowed image size expressed as WIDTHxHEIGHT (e.g. 640x480). Leave blank for no restriction. If a larger image is uploaded, it will be resized to reflect the given width and height. Resizing images on upload will cause the loss of <a href="http://en.wikipedia.org/wiki/Exchangeable_image_file_format">EXIF data</a> in the image.'),
  79. );
  80. $form['max_resolution']['x'] = array(
  81. '#type' => 'textfield',
  82. '#title' => t('Maximum width'),
  83. '#title_display' => 'invisible',
  84. '#default_value' => $max_resolution[0],
  85. '#size' => 5,
  86. '#maxlength' => 5,
  87. '#field_suffix' => ' x ',
  88. );
  89. $form['max_resolution']['y'] = array(
  90. '#type' => 'textfield',
  91. '#title' => t('Maximum height'),
  92. '#title_display' => 'invisible',
  93. '#default_value' => $max_resolution[1],
  94. '#size' => 5,
  95. '#maxlength' => 5,
  96. '#field_suffix' => ' ' . t('pixels'),
  97. );
  98. $min_resolution = explode('x', $settings['min_resolution']) + array('', '');
  99. $form['min_resolution'] = array(
  100. '#type' => 'item',
  101. '#title' => t('Minimum image resolution'),
  102. '#element_validate' => array('_image_field_resolution_validate'),
  103. '#weight' => 4.2,
  104. '#field_prefix' => '<div class="container-inline">',
  105. '#field_suffix' => '</div>',
  106. '#description' => t('The minimum allowed image size expressed as WIDTHxHEIGHT (e.g. 640x480). Leave blank for no restriction. If a smaller image is uploaded, it will be rejected.'),
  107. );
  108. $form['min_resolution']['x'] = array(
  109. '#type' => 'textfield',
  110. '#title' => t('Minimum width'),
  111. '#title_display' => 'invisible',
  112. '#default_value' => $min_resolution[0],
  113. '#size' => 5,
  114. '#maxlength' => 5,
  115. '#field_suffix' => ' x ',
  116. );
  117. $form['min_resolution']['y'] = array(
  118. '#type' => 'textfield',
  119. '#title' => t('Minimum height'),
  120. '#title_display' => 'invisible',
  121. '#default_value' => $min_resolution[1],
  122. '#size' => 5,
  123. '#maxlength' => 5,
  124. '#field_suffix' => ' ' . t('pixels'),
  125. );
  126. // Remove the description option.
  127. unset($form['description_field']);
  128. // Add title and alt configuration options.
  129. $form['alt_field'] = array(
  130. '#type' => 'checkbox',
  131. '#title' => t('Enable <em>Alt</em> field'),
  132. '#default_value' => $settings['alt_field'],
  133. '#description' => t('The alt attribute may be used by search engines, screen readers, and when the image cannot be loaded.'),
  134. '#weight' => 10,
  135. );
  136. $form['title_field'] = array(
  137. '#type' => 'checkbox',
  138. '#title' => t('Enable <em>Title</em> field'),
  139. '#default_value' => $settings['title_field'],
  140. '#description' => t('The title attribute is used as a tooltip when the mouse hovers over the image.'),
  141. '#weight' => 11,
  142. );
  143. // Add the default image to the instance.
  144. $form['default_image'] = array(
  145. '#title' => t('Default image'),
  146. '#type' => 'managed_file',
  147. '#description' => t("If no image is uploaded, this image will be shown on display and will override the field's default image."),
  148. '#default_value' => $settings['default_image'],
  149. '#upload_location' => $field['settings']['uri_scheme'] . '://default_images/',
  150. );
  151. return $form;
  152. }
  153. /**
  154. * Element validate function for resolution fields.
  155. */
  156. function _image_field_resolution_validate($element, &$form_state) {
  157. if (!empty($element['x']['#value']) || !empty($element['y']['#value'])) {
  158. foreach (array('x', 'y') as $dimension) {
  159. $value = $element[$dimension]['#value'];
  160. if (!is_numeric($value)) {
  161. form_error($element[$dimension], t('Height and width values must be numeric.'));
  162. return;
  163. }
  164. if (intval($value) == 0) {
  165. form_error($element[$dimension], t('Both a height and width value must be specified in the !name field.', array('!name' => $element['#title'])));
  166. return;
  167. }
  168. }
  169. form_set_value($element, intval($element['x']['#value']) . 'x' . intval($element['y']['#value']), $form_state);
  170. }
  171. else {
  172. form_set_value($element, '', $form_state);
  173. }
  174. }
  175. /**
  176. * Implements hook_field_load().
  177. */
  178. function image_field_load($entity_type, $entities, $field, $instances, $langcode, &$items, $age) {
  179. file_field_load($entity_type, $entities, $field, $instances, $langcode, $items, $age);
  180. }
  181. /**
  182. * Implements hook_field_prepare_view().
  183. */
  184. function image_field_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items) {
  185. // If there are no files specified at all, use the default.
  186. foreach ($entities as $id => $entity) {
  187. if (empty($items[$id])) {
  188. $fid = 0;
  189. // Use the default for the instance if one is available.
  190. if (!empty($instances[$id]['settings']['default_image'])) {
  191. $fid = $instances[$id]['settings']['default_image'];
  192. }
  193. // Otherwise, use the default for the field.
  194. elseif (!empty($field['settings']['default_image'])) {
  195. $fid = $field['settings']['default_image'];
  196. }
  197. // Add the default image if one is found.
  198. if ($fid && ($file = file_load($fid))) {
  199. $items[$id][0] = (array) $file + array(
  200. 'is_default' => TRUE,
  201. 'alt' => '',
  202. 'title' => '',
  203. );
  204. }
  205. }
  206. }
  207. }
  208. /**
  209. * Implements hook_field_presave().
  210. */
  211. function image_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
  212. file_field_presave($entity_type, $entity, $field, $instance, $langcode, $items);
  213. // Determine the dimensions if necessary.
  214. foreach ($items as &$item) {
  215. if (!isset($item['width']) || !isset($item['height'])) {
  216. $info = image_get_info(file_load($item['fid'])->uri);
  217. if (is_array($info)) {
  218. $item['width'] = $info['width'];
  219. $item['height'] = $info['height'];
  220. }
  221. }
  222. }
  223. }
  224. /**
  225. * Implements hook_field_insert().
  226. */
  227. function image_field_insert($entity_type, $entity, $field, $instance, $langcode, &$items) {
  228. file_field_insert($entity_type, $entity, $field, $instance, $langcode, $items);
  229. }
  230. /**
  231. * Implements hook_field_update().
  232. */
  233. function image_field_update($entity_type, $entity, $field, $instance, $langcode, &$items) {
  234. file_field_update($entity_type, $entity, $field, $instance, $langcode, $items);
  235. }
  236. /**
  237. * Implements hook_field_delete().
  238. */
  239. function image_field_delete($entity_type, $entity, $field, $instance, $langcode, &$items) {
  240. file_field_delete($entity_type, $entity, $field, $instance, $langcode, $items);
  241. }
  242. /**
  243. * Implements hook_field_delete_revision().
  244. */
  245. function image_field_delete_revision($entity_type, $entity, $field, $instance, $langcode, &$items) {
  246. file_field_delete_revision($entity_type, $entity, $field, $instance, $langcode, $items);
  247. }
  248. /**
  249. * Implements hook_field_is_empty().
  250. */
  251. function image_field_is_empty($item, $field) {
  252. return file_field_is_empty($item, $field);
  253. }
  254. /**
  255. * Implements hook_field_widget_info().
  256. */
  257. function image_field_widget_info() {
  258. return array(
  259. 'image_image' => array(
  260. 'label' => t('Image'),
  261. 'field types' => array('image'),
  262. 'settings' => array(
  263. 'progress_indicator' => 'throbber',
  264. 'preview_image_style' => 'thumbnail',
  265. ),
  266. 'behaviors' => array(
  267. 'multiple values' => FIELD_BEHAVIOR_CUSTOM,
  268. 'default value' => FIELD_BEHAVIOR_NONE,
  269. ),
  270. ),
  271. );
  272. }
  273. /**
  274. * Implements hook_field_widget_settings_form().
  275. */
  276. function image_field_widget_settings_form($field, $instance) {
  277. $widget = $instance['widget'];
  278. $settings = $widget['settings'];
  279. // Use the file widget settings form.
  280. $form = file_field_widget_settings_form($field, $instance);
  281. $form['preview_image_style'] = array(
  282. '#title' => t('Preview image style'),
  283. '#type' => 'select',
  284. '#options' => image_style_options(FALSE, PASS_THROUGH),
  285. '#empty_option' => '<' . t('no preview') . '>',
  286. '#default_value' => $settings['preview_image_style'],
  287. '#description' => t('The preview image will be shown while editing the content.'),
  288. '#weight' => 15,
  289. );
  290. return $form;
  291. }
  292. /**
  293. * Implements hook_field_widget_form().
  294. */
  295. function image_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  296. // Add display_field setting to field because file_field_widget_form() assumes it is set.
  297. $field['settings']['display_field'] = 0;
  298. $elements = file_field_widget_form($form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  299. $settings = $instance['settings'];
  300. foreach (element_children($elements) as $delta) {
  301. // Add upload resolution validation.
  302. if ($settings['max_resolution'] || $settings['min_resolution']) {
  303. $elements[$delta]['#upload_validators']['file_validate_image_resolution'] = array($settings['max_resolution'], $settings['min_resolution']);
  304. }
  305. // If not using custom extension validation, ensure this is an image.
  306. $supported_extensions = array('png', 'gif', 'jpg', 'jpeg');
  307. $extensions = isset($elements[$delta]['#upload_validators']['file_validate_extensions'][0]) ? $elements[$delta]['#upload_validators']['file_validate_extensions'][0] : implode(' ', $supported_extensions);
  308. $extensions = array_intersect(explode(' ', $extensions), $supported_extensions);
  309. $elements[$delta]['#upload_validators']['file_validate_extensions'][0] = implode(' ', $extensions);
  310. // Add all extra functionality provided by the image widget.
  311. $elements[$delta]['#process'][] = 'image_field_widget_process';
  312. }
  313. if ($field['cardinality'] == 1) {
  314. // If there's only one field, return it as delta 0.
  315. if (empty($elements[0]['#default_value']['fid'])) {
  316. $elements[0]['#description'] = theme('file_upload_help', array('description' => $instance['description'], 'upload_validators' => $elements[0]['#upload_validators']));
  317. }
  318. }
  319. else {
  320. $elements['#file_upload_description'] = theme('file_upload_help', array('upload_validators' => $elements[0]['#upload_validators']));
  321. }
  322. return $elements;
  323. }
  324. /**
  325. * An element #process callback for the image_image field type.
  326. *
  327. * Expands the image_image type to include the alt and title fields.
  328. */
  329. function image_field_widget_process($element, &$form_state, $form) {
  330. $item = $element['#value'];
  331. $item['fid'] = $element['fid']['#value'];
  332. $instance = field_widget_instance($element, $form_state);
  333. $settings = $instance['settings'];
  334. $widget_settings = $instance['widget']['settings'];
  335. $element['#theme'] = 'image_widget';
  336. $element['#attached']['css'][] = drupal_get_path('module', 'image') . '/image.css';
  337. // Add the image preview.
  338. if ($element['#file'] && $widget_settings['preview_image_style']) {
  339. $variables = array(
  340. 'style_name' => $widget_settings['preview_image_style'],
  341. 'path' => $element['#file']->uri,
  342. );
  343. // Determine image dimensions.
  344. if (isset($element['#value']['width']) && isset($element['#value']['height'])) {
  345. $variables['width'] = $element['#value']['width'];
  346. $variables['height'] = $element['#value']['height'];
  347. }
  348. else {
  349. $info = image_get_info($element['#file']->uri);
  350. if (is_array($info)) {
  351. $variables['width'] = $info['width'];
  352. $variables['height'] = $info['height'];
  353. }
  354. else {
  355. $variables['width'] = $variables['height'] = NULL;
  356. }
  357. }
  358. $element['preview'] = array(
  359. '#type' => 'markup',
  360. '#markup' => theme('image_style', $variables),
  361. );
  362. // Store the dimensions in the form so the file doesn't have to be accessed
  363. // again. This is important for remote files.
  364. $element['width'] = array(
  365. '#type' => 'hidden',
  366. '#value' => $variables['width'],
  367. );
  368. $element['height'] = array(
  369. '#type' => 'hidden',
  370. '#value' => $variables['height'],
  371. );
  372. }
  373. // Add the additional alt and title fields.
  374. $element['alt'] = array(
  375. '#title' => t('Alternate text'),
  376. '#type' => 'textfield',
  377. '#default_value' => isset($item['alt']) ? $item['alt'] : '',
  378. '#description' => t('This text will be used by screen readers, search engines, or when the image cannot be loaded.'),
  379. // @see http://www.gawds.org/show.php?contentid=28
  380. '#maxlength' => 512,
  381. '#weight' => -2,
  382. '#access' => (bool) $item['fid'] && $settings['alt_field'],
  383. );
  384. $element['title'] = array(
  385. '#type' => 'textfield',
  386. '#title' => t('Title'),
  387. '#default_value' => isset($item['title']) ? $item['title'] : '',
  388. '#description' => t('The title is used as a tool tip when the user hovers the mouse over the image.'),
  389. '#maxlength' => 1024,
  390. '#weight' => -1,
  391. '#access' => (bool) $item['fid'] && $settings['title_field'],
  392. );
  393. return $element;
  394. }
  395. /**
  396. * Returns HTML for an image field widget.
  397. *
  398. * @param $variables
  399. * An associative array containing:
  400. * - element: A render element representing the image field widget.
  401. *
  402. * @ingroup themeable
  403. */
  404. function theme_image_widget($variables) {
  405. $element = $variables['element'];
  406. $output = '';
  407. $output .= '<div class="image-widget form-managed-file clearfix">';
  408. if (isset($element['preview'])) {
  409. $output .= '<div class="image-preview">';
  410. $output .= drupal_render($element['preview']);
  411. $output .= '</div>';
  412. }
  413. $output .= '<div class="image-widget-data">';
  414. if ($element['fid']['#value'] != 0) {
  415. $element['filename']['#markup'] .= ' <span class="file-size">(' . format_size($element['#file']->filesize) . ')</span> ';
  416. }
  417. $output .= drupal_render_children($element);
  418. $output .= '</div>';
  419. $output .= '</div>';
  420. return $output;
  421. }
  422. /**
  423. * Implements hook_field_formatter_info().
  424. */
  425. function image_field_formatter_info() {
  426. $formatters = array(
  427. 'image' => array(
  428. 'label' => t('Image'),
  429. 'field types' => array('image'),
  430. 'settings' => array('image_style' => '', 'image_link' => ''),
  431. ),
  432. );
  433. return $formatters;
  434. }
  435. /**
  436. * Implements hook_field_formatter_settings_form().
  437. */
  438. function image_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  439. $display = $instance['display'][$view_mode];
  440. $settings = $display['settings'];
  441. $image_styles = image_style_options(FALSE, PASS_THROUGH);
  442. $element['image_style'] = array(
  443. '#title' => t('Image style'),
  444. '#type' => 'select',
  445. '#default_value' => $settings['image_style'],
  446. '#empty_option' => t('None (original image)'),
  447. '#options' => $image_styles,
  448. );
  449. $link_types = array(
  450. 'content' => t('Content'),
  451. 'file' => t('File'),
  452. );
  453. $element['image_link'] = array(
  454. '#title' => t('Link image to'),
  455. '#type' => 'select',
  456. '#default_value' => $settings['image_link'],
  457. '#empty_option' => t('Nothing'),
  458. '#options' => $link_types,
  459. );
  460. return $element;
  461. }
  462. /**
  463. * Implements hook_field_formatter_settings_summary().
  464. */
  465. function image_field_formatter_settings_summary($field, $instance, $view_mode) {
  466. $display = $instance['display'][$view_mode];
  467. $settings = $display['settings'];
  468. $summary = array();
  469. $image_styles = image_style_options(FALSE, PASS_THROUGH);
  470. // Unset possible 'No defined styles' option.
  471. unset($image_styles['']);
  472. // Styles could be lost because of enabled/disabled modules that defines
  473. // their styles in code.
  474. if (isset($image_styles[$settings['image_style']])) {
  475. $summary[] = t('Image style: @style', array('@style' => $image_styles[$settings['image_style']]));
  476. }
  477. else {
  478. $summary[] = t('Original image');
  479. }
  480. $link_types = array(
  481. 'content' => t('Linked to content'),
  482. 'file' => t('Linked to file'),
  483. );
  484. // Display this setting only if image is linked.
  485. if (isset($link_types[$settings['image_link']])) {
  486. $summary[] = $link_types[$settings['image_link']];
  487. }
  488. return implode('<br />', $summary);
  489. }
  490. /**
  491. * Implements hook_field_formatter_view().
  492. */
  493. function image_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  494. $element = array();
  495. // Check if the formatter involves a link.
  496. if ($display['settings']['image_link'] == 'content') {
  497. $uri = entity_uri($entity_type, $entity);
  498. }
  499. elseif ($display['settings']['image_link'] == 'file') {
  500. $link_file = TRUE;
  501. }
  502. foreach ($items as $delta => $item) {
  503. if (isset($link_file)) {
  504. $uri = array(
  505. 'path' => file_create_url($item['uri']),
  506. 'options' => array(),
  507. );
  508. }
  509. $element[$delta] = array(
  510. '#theme' => 'image_formatter',
  511. '#item' => $item,
  512. '#image_style' => $display['settings']['image_style'],
  513. '#path' => isset($uri) ? $uri : '',
  514. );
  515. }
  516. return $element;
  517. }
  518. /**
  519. * Returns HTML for an image field formatter.
  520. *
  521. * @param $variables
  522. * An associative array containing:
  523. * - item: Associative array of image data, which may include "uri", "alt",
  524. * "width", "height", "title" and "attributes".
  525. * - image_style: An optional image style.
  526. * - path: An array containing the link 'path' and link 'options'.
  527. *
  528. * @ingroup themeable
  529. */
  530. function theme_image_formatter($variables) {
  531. $item = $variables['item'];
  532. $image = array(
  533. 'path' => $item['uri'],
  534. );
  535. if (array_key_exists('alt', $item)) {
  536. $image['alt'] = $item['alt'];
  537. }
  538. if (isset($item['attributes'])) {
  539. $image['attributes'] = $item['attributes'];
  540. }
  541. if (isset($item['width']) && isset($item['height'])) {
  542. $image['width'] = $item['width'];
  543. $image['height'] = $item['height'];
  544. }
  545. // Do not output an empty 'title' attribute.
  546. if (isset($item['title']) && drupal_strlen($item['title']) > 0) {
  547. $image['title'] = $item['title'];
  548. }
  549. if ($variables['image_style']) {
  550. $image['style_name'] = $variables['image_style'];
  551. $output = theme('image_style', $image);
  552. }
  553. else {
  554. $output = theme('image', $image);
  555. }
  556. // The link path and link options are both optional, but for the options to be
  557. // processed, the link path must at least be an empty string.
  558. if (isset($variables['path']['path'])) {
  559. $path = $variables['path']['path'];
  560. $options = isset($variables['path']['options']) ? $variables['path']['options'] : array();
  561. // When displaying an image inside a link, the html option must be TRUE.
  562. $options['html'] = TRUE;
  563. $output = l($output, $path, $options);
  564. }
  565. return $output;
  566. }