image.field.inc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  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' => '[date:custom:Y]-[date:custom:m]',
  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('Height and width values must be positive numbers.'));
  166. return;
  167. }
  168. if (intval($value) == 0) {
  169. form_error($element[$dimension], t('Both a height and width value must be specified in the !name field.', array('!name' => $element['#title'])));
  170. return;
  171. }
  172. }
  173. form_set_value($element, intval($element['x']['#value']) . 'x' . intval($element['y']['#value']), $form_state);
  174. }
  175. else {
  176. form_set_value($element, '', $form_state);
  177. }
  178. }
  179. /**
  180. * Implements hook_field_load().
  181. */
  182. function image_field_load($entity_type, $entities, $field, $instances, $langcode, &$items, $age) {
  183. file_field_load($entity_type, $entities, $field, $instances, $langcode, $items, $age);
  184. }
  185. /**
  186. * Implements hook_field_prepare_view().
  187. */
  188. function image_field_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items) {
  189. // If there are no files specified at all, use the default.
  190. foreach ($entities as $id => $entity) {
  191. if (empty($items[$id])) {
  192. $fid = 0;
  193. // Use the default for the instance if one is available.
  194. if (!empty($instances[$id]['settings']['default_image'])) {
  195. $fid = $instances[$id]['settings']['default_image'];
  196. }
  197. // Otherwise, use the default for the field.
  198. elseif (!empty($field['settings']['default_image'])) {
  199. $fid = $field['settings']['default_image'];
  200. }
  201. // Add the default image if one is found.
  202. if ($fid && ($file = file_load($fid))) {
  203. $items[$id][0] = (array) $file + array(
  204. 'is_default' => TRUE,
  205. 'alt' => '',
  206. 'title' => '',
  207. );
  208. }
  209. }
  210. }
  211. }
  212. /**
  213. * Implements hook_field_presave().
  214. */
  215. function image_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
  216. file_field_presave($entity_type, $entity, $field, $instance, $langcode, $items);
  217. // Determine the dimensions if necessary.
  218. foreach ($items as &$item) {
  219. if (!isset($item['width']) || !isset($item['height'])) {
  220. $info = image_get_info(file_load($item['fid'])->uri);
  221. if (is_array($info)) {
  222. $item['width'] = $info['width'];
  223. $item['height'] = $info['height'];
  224. }
  225. }
  226. }
  227. }
  228. /**
  229. * Implements hook_field_insert().
  230. */
  231. function image_field_insert($entity_type, $entity, $field, $instance, $langcode, &$items) {
  232. file_field_insert($entity_type, $entity, $field, $instance, $langcode, $items);
  233. }
  234. /**
  235. * Implements hook_field_update().
  236. */
  237. function image_field_update($entity_type, $entity, $field, $instance, $langcode, &$items) {
  238. file_field_update($entity_type, $entity, $field, $instance, $langcode, $items);
  239. }
  240. /**
  241. * Implements hook_field_delete().
  242. */
  243. function image_field_delete($entity_type, $entity, $field, $instance, $langcode, &$items) {
  244. file_field_delete($entity_type, $entity, $field, $instance, $langcode, $items);
  245. }
  246. /**
  247. * Implements hook_field_delete_revision().
  248. */
  249. function image_field_delete_revision($entity_type, $entity, $field, $instance, $langcode, &$items) {
  250. file_field_delete_revision($entity_type, $entity, $field, $instance, $langcode, $items);
  251. }
  252. /**
  253. * Implements hook_field_is_empty().
  254. */
  255. function image_field_is_empty($item, $field) {
  256. return file_field_is_empty($item, $field);
  257. }
  258. /**
  259. * Implements hook_field_widget_info().
  260. */
  261. function image_field_widget_info() {
  262. return array(
  263. 'image_image' => array(
  264. 'label' => t('Image'),
  265. 'field types' => array('image'),
  266. 'settings' => array(
  267. 'progress_indicator' => 'throbber',
  268. 'preview_image_style' => 'thumbnail',
  269. ),
  270. 'behaviors' => array(
  271. 'multiple values' => FIELD_BEHAVIOR_CUSTOM,
  272. 'default value' => FIELD_BEHAVIOR_NONE,
  273. ),
  274. ),
  275. );
  276. }
  277. /**
  278. * Implements hook_field_widget_settings_form().
  279. */
  280. function image_field_widget_settings_form($field, $instance) {
  281. $widget = $instance['widget'];
  282. $settings = $widget['settings'];
  283. // Use the file widget settings form.
  284. $form = file_field_widget_settings_form($field, $instance);
  285. $form['preview_image_style'] = array(
  286. '#title' => t('Preview image style'),
  287. '#type' => 'select',
  288. '#options' => image_style_options(FALSE, PASS_THROUGH),
  289. '#empty_option' => '<' . t('no preview') . '>',
  290. '#default_value' => $settings['preview_image_style'],
  291. '#description' => t('The preview image will be shown while editing the content.'),
  292. '#weight' => 15,
  293. );
  294. return $form;
  295. }
  296. /**
  297. * Implements hook_field_widget_form().
  298. */
  299. function image_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  300. // Add display_field setting to field because file_field_widget_form() assumes it is set.
  301. $field['settings']['display_field'] = 0;
  302. $elements = file_field_widget_form($form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  303. $settings = $instance['settings'];
  304. foreach (element_children($elements) as $delta) {
  305. // Add upload resolution validation.
  306. if ($settings['max_resolution'] || $settings['min_resolution']) {
  307. $elements[$delta]['#upload_validators']['file_validate_image_resolution'] = array($settings['max_resolution'], $settings['min_resolution']);
  308. }
  309. // If not using custom extension validation, ensure this is an image.
  310. $supported_extensions = array('png', 'gif', 'jpg', 'jpeg');
  311. $extensions = isset($elements[$delta]['#upload_validators']['file_validate_extensions'][0]) ? $elements[$delta]['#upload_validators']['file_validate_extensions'][0] : implode(' ', $supported_extensions);
  312. $extensions = array_intersect(explode(' ', $extensions), $supported_extensions);
  313. $elements[$delta]['#upload_validators']['file_validate_extensions'][0] = implode(' ', $extensions);
  314. $elements[$delta]['#upload_validators']['file_validate_is_image'] = array();
  315. // Add all extra functionality provided by the image widget.
  316. $elements[$delta]['#process'][] = 'image_field_widget_process';
  317. }
  318. if ($field['cardinality'] == 1) {
  319. // If there's only one field, return it as delta 0.
  320. if (empty($elements[0]['#default_value']['fid'])) {
  321. $elements[0]['#description'] = theme('file_upload_help', array('description' => field_filter_xss($instance['description']), 'upload_validators' => $elements[0]['#upload_validators']));
  322. }
  323. }
  324. else {
  325. $elements['#file_upload_description'] = theme('file_upload_help', array('upload_validators' => $elements[0]['#upload_validators']));
  326. }
  327. return $elements;
  328. }
  329. /**
  330. * An element #process callback for the image_image field type.
  331. *
  332. * Expands the image_image type to include the alt and title fields.
  333. */
  334. function image_field_widget_process($element, &$form_state, $form) {
  335. $item = $element['#value'];
  336. $item['fid'] = $element['fid']['#value'];
  337. $instance = field_widget_instance($element, $form_state);
  338. $settings = $instance['settings'];
  339. $widget_settings = $instance['widget']['settings'];
  340. $element['#theme'] = 'image_widget';
  341. $element['#attached']['css'][] = drupal_get_path('module', 'image') . '/image.css';
  342. // Add the image preview.
  343. if ($element['#file'] && $widget_settings['preview_image_style']) {
  344. $variables = array(
  345. 'style_name' => $widget_settings['preview_image_style'],
  346. 'path' => $element['#file']->uri,
  347. );
  348. // Determine image dimensions.
  349. if (isset($element['#value']['width']) && isset($element['#value']['height'])) {
  350. $variables['width'] = $element['#value']['width'];
  351. $variables['height'] = $element['#value']['height'];
  352. }
  353. else {
  354. $info = image_get_info($element['#file']->uri);
  355. if (is_array($info)) {
  356. $variables['width'] = $info['width'];
  357. $variables['height'] = $info['height'];
  358. }
  359. else {
  360. $variables['width'] = $variables['height'] = NULL;
  361. }
  362. }
  363. $element['preview'] = array(
  364. '#type' => 'markup',
  365. '#markup' => theme('image_style', $variables),
  366. );
  367. // Store the dimensions in the form so the file doesn't have to be accessed
  368. // again. This is important for remote files.
  369. $element['width'] = array(
  370. '#type' => 'hidden',
  371. '#value' => $variables['width'],
  372. );
  373. $element['height'] = array(
  374. '#type' => 'hidden',
  375. '#value' => $variables['height'],
  376. );
  377. }
  378. // Add the additional alt and title fields.
  379. $element['alt'] = array(
  380. '#title' => t('Alternate text'),
  381. '#type' => 'textfield',
  382. '#default_value' => isset($item['alt']) ? $item['alt'] : '',
  383. '#description' => t('This text will be used by screen readers, search engines, or when the image cannot be loaded.'),
  384. // @see http://www.gawds.org/show.php?contentid=28
  385. '#maxlength' => 512,
  386. '#weight' => -2,
  387. '#access' => (bool) $item['fid'] && $settings['alt_field'],
  388. );
  389. $element['title'] = array(
  390. '#type' => 'textfield',
  391. '#title' => t('Title'),
  392. '#default_value' => isset($item['title']) ? $item['title'] : '',
  393. '#description' => t('The title is used as a tool tip when the user hovers the mouse over the image.'),
  394. '#maxlength' => 1024,
  395. '#weight' => -1,
  396. '#access' => (bool) $item['fid'] && $settings['title_field'],
  397. );
  398. return $element;
  399. }
  400. /**
  401. * Returns HTML for an image field widget.
  402. *
  403. * @param $variables
  404. * An associative array containing:
  405. * - element: A render element representing the image field widget.
  406. *
  407. * @ingroup themeable
  408. */
  409. function theme_image_widget($variables) {
  410. $element = $variables['element'];
  411. $output = '';
  412. $output .= '<div class="image-widget form-managed-file clearfix">';
  413. if (isset($element['preview'])) {
  414. $output .= '<div class="image-preview">';
  415. $output .= drupal_render($element['preview']);
  416. $output .= '</div>';
  417. }
  418. $output .= '<div class="image-widget-data">';
  419. if ($element['fid']['#value'] != 0) {
  420. $element['filename']['#markup'] .= ' <span class="file-size">(' . format_size($element['#file']->filesize) . ')</span> ';
  421. }
  422. $output .= drupal_render_children($element);
  423. $output .= '</div>';
  424. $output .= '</div>';
  425. return $output;
  426. }
  427. /**
  428. * Implements hook_field_formatter_info().
  429. */
  430. function image_field_formatter_info() {
  431. $formatters = array(
  432. 'image' => array(
  433. 'label' => t('Image'),
  434. 'field types' => array('image'),
  435. 'settings' => array('image_style' => '', 'image_link' => ''),
  436. ),
  437. );
  438. return $formatters;
  439. }
  440. /**
  441. * Implements hook_field_formatter_settings_form().
  442. */
  443. function image_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  444. $display = $instance['display'][$view_mode];
  445. $settings = $display['settings'];
  446. $image_styles = image_style_options(FALSE, PASS_THROUGH);
  447. $element['image_style'] = array(
  448. '#title' => t('Image style'),
  449. '#type' => 'select',
  450. '#default_value' => $settings['image_style'],
  451. '#empty_option' => t('None (original image)'),
  452. '#options' => $image_styles,
  453. );
  454. $link_types = array(
  455. 'content' => t('Content'),
  456. 'file' => t('File'),
  457. );
  458. $element['image_link'] = array(
  459. '#title' => t('Link image to'),
  460. '#type' => 'select',
  461. '#default_value' => $settings['image_link'],
  462. '#empty_option' => t('Nothing'),
  463. '#options' => $link_types,
  464. );
  465. return $element;
  466. }
  467. /**
  468. * Implements hook_field_formatter_settings_summary().
  469. */
  470. function image_field_formatter_settings_summary($field, $instance, $view_mode) {
  471. $display = $instance['display'][$view_mode];
  472. $settings = $display['settings'];
  473. $summary = array();
  474. $image_styles = image_style_options(FALSE, PASS_THROUGH);
  475. // Unset possible 'No defined styles' option.
  476. unset($image_styles['']);
  477. // Styles could be lost because of enabled/disabled modules that defines
  478. // their styles in code.
  479. if (isset($image_styles[$settings['image_style']])) {
  480. $summary[] = t('Image style: @style', array('@style' => $image_styles[$settings['image_style']]));
  481. }
  482. else {
  483. $summary[] = t('Original image');
  484. }
  485. $link_types = array(
  486. 'content' => t('Linked to content'),
  487. 'file' => t('Linked to file'),
  488. );
  489. // Display this setting only if image is linked.
  490. if (isset($link_types[$settings['image_link']])) {
  491. $summary[] = $link_types[$settings['image_link']];
  492. }
  493. return implode('<br />', $summary);
  494. }
  495. /**
  496. * Implements hook_field_formatter_view().
  497. */
  498. function image_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  499. $element = array();
  500. // Check if the formatter involves a link.
  501. if ($display['settings']['image_link'] == 'content') {
  502. $uri = entity_uri($entity_type, $entity);
  503. }
  504. elseif ($display['settings']['image_link'] == 'file') {
  505. $link_file = TRUE;
  506. }
  507. foreach ($items as $delta => $item) {
  508. if (isset($link_file)) {
  509. $uri = array(
  510. 'path' => file_create_url($item['uri']),
  511. 'options' => array(),
  512. );
  513. }
  514. $element[$delta] = array(
  515. '#theme' => 'image_formatter',
  516. '#item' => $item,
  517. '#image_style' => $display['settings']['image_style'],
  518. '#path' => isset($uri) ? $uri : '',
  519. );
  520. }
  521. return $element;
  522. }
  523. /**
  524. * Returns HTML for an image field formatter.
  525. *
  526. * @param $variables
  527. * An associative array containing:
  528. * - item: Associative array of image data, which may include "uri", "alt",
  529. * "width", "height", "title" and "attributes".
  530. * - image_style: An optional image style.
  531. * - path: An array containing the link 'path' and link 'options'.
  532. *
  533. * @ingroup themeable
  534. */
  535. function theme_image_formatter($variables) {
  536. $item = $variables['item'];
  537. $image = array(
  538. 'path' => $item['uri'],
  539. );
  540. if (array_key_exists('alt', $item)) {
  541. $image['alt'] = $item['alt'];
  542. }
  543. if (isset($item['attributes'])) {
  544. $image['attributes'] = $item['attributes'];
  545. }
  546. if (isset($item['width']) && isset($item['height'])) {
  547. $image['width'] = $item['width'];
  548. $image['height'] = $item['height'];
  549. }
  550. // Do not output an empty 'title' attribute.
  551. if (isset($item['title']) && drupal_strlen($item['title']) > 0) {
  552. $image['title'] = $item['title'];
  553. }
  554. if ($variables['image_style']) {
  555. $image['style_name'] = $variables['image_style'];
  556. $output = theme('image_style', $image);
  557. }
  558. else {
  559. $output = theme('image', $image);
  560. }
  561. // The link path and link options are both optional, but for the options to be
  562. // processed, the link path must at least be an empty string.
  563. if (isset($variables['path']['path'])) {
  564. $path = $variables['path']['path'];
  565. $options = isset($variables['path']['options']) ? $variables['path']['options'] : array();
  566. // When displaying an image inside a link, the html option must be TRUE.
  567. $options['html'] = TRUE;
  568. $output = l($output, $path, $options);
  569. }
  570. return $output;
  571. }