iframe.module 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. <?php
  2. /**
  3. * @file
  4. * Defines an iframe field with all attributes.
  5. *
  6. * Modul-programming
  7. * see also http://de.slideshare.net/zugec/fields-in-core-how-to-create-a-custom-field
  8. */
  9. define('iframe_EXTERNAL', 'external');
  10. define('iframe_INTERNAL', 'internal');
  11. define('iframe_FRONT', 'front');
  12. define('iframe_EMAIL', 'email');
  13. define('iframe_DOMAINS', 'aero|arpa|biz|com|cat|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel|mobi|local');
  14. define('DEBUG_LEVEL', 0);
  15. if (!defined('DEBUG_LEVEL')) {
  16. define('DEBUG_LEVEL', 4);
  17. }
  18. /* function "dpr" is from devel-module, we will use it if exists */
  19. function iframe_debug($level, $func = 'iframe', $debugvar = NULL) {
  20. if ($level <= DEBUG_LEVEL) {
  21. $debugtitle = '[iframe(' . $level . ')] ' . $func;
  22. if (!function_exists('kpr') && is_readable('../devel/devel.module')) {
  23. include_once('../devel/devel.module');
  24. }
  25. if (function_exists('kpr')) {
  26. kpr(array($debugtitle, $debugvar), NULL, $debugtitle);
  27. }
  28. else {
  29. drupal_set_message($debugtitle . ' ' . print_r($debugvar, TRUE), 'warning');
  30. }
  31. }
  32. }
  33. /**
  34. * Implements hook_help().
  35. */
  36. function iframe_help($path, $arg) {
  37. switch ($path) {
  38. case 'admin/help#iframe':
  39. $output = '';
  40. $output .= '<h3>' . t('About') . '</h3>';
  41. $output .= '<p>' . t('The Iframe module defines an iframe field type for the Field module. Further definable are attributes for styling the iframe, like: URL, width, height, title, class, frameborder, scrolling and transparency. See the <a href="@field-help">Field module help page</a> for more information about fields.', array('@field-help' => url('admin/help/field'))) . '</p>';
  42. return $output;
  43. }
  44. }
  45. /***************************************************************
  46. * Field Type API hooks
  47. ***************************************************************/
  48. /**
  49. * Implements hook_field_info().
  50. * Shows the "Field type" while adding a new field to the content-type
  51. * (Step 1)
  52. */
  53. function iframe_field_info() {
  54. return array(
  55. 'iframe' => array(
  56. 'label' => t('Iframe'),
  57. 'description' => t('This field stores the parameters for an iframe.'),
  58. /* define defaults either in 'settings' (globally) OR in 'instance_settings' (per content-type) */
  59. // settings - used as default values for the general field definition
  60. 'settings' => array(
  61. #'title' => '',
  62. #'url' => '',
  63. #'width' => '',
  64. #'height' => '',
  65. 'class' => 'field-iframe', /* class-name is the only attribute, which we use additive */
  66. 'frameborder' => 0, /* "0" or "yes" */
  67. 'scrolling' => 'auto',
  68. 'transparency' => 0,
  69. 'tokensupport' => 0,
  70. #'expose_class' => 1,
  71. #'allowfullscreen' => 0,
  72. ),
  73. 'instance_settings' => array(
  74. #'title' => '',
  75. #'url' => '',
  76. 'width' => '100%',
  77. 'height' => '800',
  78. 'class' => 'field-iframe-instance',
  79. #'frameborder' => 0, /* "0" or "yes" */
  80. #'scrolling' => 'auto',
  81. #'transparency' => 0,
  82. #'tokensupport' => 0,
  83. 'expose_class' => 1,
  84. 'allowfullscreen' => 0,
  85. ),
  86. 'default_widget' => 'iframe_widget_url',
  87. 'default_formatter' => 'iframe_formatter_default',
  88. ),
  89. );
  90. }
  91. /**
  92. * Implements hook_field_settings_form().
  93. * These settings apply to the iframe field everywhere(!) it is used. These settings impact the way that data is stored in the database and cannot be changed once data has been created.
  94. * Field XYZ sub-menu "Field settings"
  95. * (Step 2)
  96. */
  97. function iframe_field_settings_form($field, $instance, $has_data) {
  98. $settings = $field['settings']; // as defined in iframe_field_info()
  99. $form = array();
  100. if ($field['type'] == 'iframe') {
  101. $form['class'] = array(
  102. '#type' => 'textfield',
  103. '#title' => t('Additional CSS Class'),
  104. '#default_value' => !empty($settings['class'])? $settings['class'] : '',
  105. '#description' => t('When output, this iframe will have this class attribute. Multiple classes should be separated by spaces.'),
  106. );
  107. $form['frameborder'] = array(
  108. '#type' => 'select',
  109. '#title' => t('Frameborder'),
  110. '#options' => array('0' => t('No frameborder'), '1' => t('Show frameborder')),
  111. '#default_value' => !empty($settings['frameborder'])? $settings['frameborder'] : 0,
  112. '#description' => t('Frameborder is the border arround the iframe. Mostly people want it silent, so the default value for frameborder is 0 = no.'),
  113. );
  114. $form['scrolling'] = array(
  115. '#type' => 'select',
  116. '#title' => t('Scrolling'),
  117. '#options' => array('auto' => t('Scrolling automatic'), 'no' => t('Scrolling disabled'), 'yes' => t('Scrolling enabled')),
  118. '#default_value' => !empty($settings['scrolling'])? $settings['scrolling'] : 'auto',
  119. '#description' => t('Scrollbars help the user to reach all iframe content despite the real height of the iframe content. Please disable it only if You know what You are doing.'),
  120. );
  121. $form['transparency'] = array(
  122. '#type' => 'select',
  123. '#title' => t('Transparency'),
  124. '#options' => array('0' => t('No transparency'), '1' => t('Allow transparency')),
  125. '#default_value' => !empty($settings['transparency'])? $settings['transparency'] : '0',
  126. '#description' => t('Allow transparency per CSS in the outer iframe tag. You have to set background-color:transparent in your iframe body tag too!'),
  127. );
  128. $form['tokensupport'] = array(
  129. '#type' => 'select',
  130. '#title' => t('Token Support'),
  131. '#options' => array('0' => t('No tokens allowed'), '1' => t('Tokens only in title field'), '2' => t('Tokens for title and url field')),
  132. '#default_value' => !empty($settings['tokensupport'])? $settings['tokensupport'] : '0',
  133. '#description' => t('Are tokens allowed for users to use in title or URL field?'),
  134. );
  135. if (! module_exists('token')) {
  136. $form['tokensupport']['#description'] .= ' ' . t('Attention: token module is not enabled currently!');
  137. }
  138. }
  139. return $form;
  140. }
  141. /**
  142. * Implements hook_field_instance_settings_form().
  143. * These settings apply only to the iframe field when used in this special content-type.
  144. * Field XYZ sub-menu "Edit"
  145. * (Step 3)
  146. */
  147. function iframe_field_instance_settings_form($field, $instance) {
  148. $field_settings = $field['settings']; // as defined before in iframe_field_settings_form() ! has preference over instance_settings
  149. $settings = $instance['settings']; // default instance_settings from iframe_field_info
  150. $form = array();
  151. if ($field['type'] == 'iframe') {
  152. $form['width'] = array(
  153. '#type' => 'textfield',
  154. '#title' => t('Iframe width'),
  155. '#default_value' => !empty($settings['width'])? $settings['width'] : (!empty($field_settings['width'])? $field_settings['width'] : ''),
  156. '#description' => t('Iframes need a fixed width and height; only numbers are allowed, or percentage values. (e.g. 500px => "500", 80% => "80%")'),
  157. //'#disabled' => $has_data,
  158. '#maxlength' => 4,
  159. '#size' => 4,
  160. );
  161. $form['height'] = array(
  162. '#type' => 'textfield',
  163. '#title' => t('Iframe height'),
  164. '#default_value' => !empty($settings['height'])? $settings['height'] : (!empty($field_settings['height'])? $field_settings['height'] : ''),
  165. '#description' => t('Iframes need a fixed width and height; only numbers are allowed, or percentage values. (e.g. 500px => "500", 80% => "80%")'),
  166. '#maxlength' => 4,
  167. '#size' => 4,
  168. );
  169. $form['class'] = array(
  170. '#type' => 'textfield',
  171. '#title' => t('Additional CSS Class'),
  172. '#default_value' => !empty($settings['class'])? $settings['class'] : (!empty($field_settings['class'])? $field_settings['class'] : ''),
  173. '#description' => t('When output, this iframe will have this class attribute. Multiple classes should be separated by spaces.'),
  174. );
  175. $form['expose_class'] = array(
  176. '#type' => 'checkbox',
  177. '#title' => t('Expose Additional CSS Class'),
  178. '#default_value' => isset($settings['expose_class'])? (int)$settings['expose_class'] : 1,
  179. '#description' => t('Allow author to specify an additional class attribute for this iframe.'),
  180. );
  181. $form['frameborder'] = array(
  182. '#type' => 'select',
  183. '#title' => t('Frameborder'),
  184. '#options' => array('0' => t('No frameborder'), '1' => t('Show frameborder')),
  185. '#default_value' => !empty($settings['frameborder'])? $settings['frameborder'] : (!empty($field_settings['frameborder'])? $field_settings['frameborder'] : 0),
  186. '#description' => t('Frameborder is the border arround the iframe. Mostly people want it silent, so the default value for frameborder is 0 = no.'),
  187. );
  188. $form['scrolling'] = array(
  189. '#type' => 'select',
  190. '#title' => t('Scrolling'),
  191. '#options' => array('auto' => t('Scrolling automatic'), 'no' => t('Scrolling disabled'), 'yes' => t('Scrolling enabled')),
  192. '#default_value' => !empty($settings['scrolling'])? $settings['scrolling'] : (!empty($field_settings['scrolling'])? $field_settings['scrolling'] : 'auto'),
  193. '#description' => t('Scrollbars help the user to reach all iframe content despite the real height of the iframe content. Please disable it only if You know what You are doing.'),
  194. );
  195. $form['transparency'] = array(
  196. '#type' => 'select',
  197. '#title' => t('Transparency'),
  198. '#options' => array('0' => t('No transparency'), '1' => t('Allow transparency')),
  199. '#default_value' => !empty($settings['transparency'])? $settings['transparency'] : (!empty($field_settings['transparency'])? $field_settings['transparency'] : 0),
  200. '#description' => t('Allow transparency per CSS in the outer iframe tag. You have to set background-color:transparent in your iframe body tag too!'),
  201. );
  202. $form['tokensupport'] = array(
  203. '#type' => 'select',
  204. '#title' => t('Token Support'),
  205. '#options' => array('0' => t('no tokens allowed'), '1' => t('tokens only in title field'), '2' => t('tokens for title and url field')),
  206. '#default_value' => !empty($settings['tokensupport'])? $settings['tokensupport'] : (!empty($field_settings['tokensupport'])? $field_settings['tokensupport'] : '0'),
  207. '#description' => t('Are tokens allowed for users to use in title or URL field?'),
  208. );
  209. if (! module_exists('token')) {
  210. $form['tokensupport']['#description'] .= ' ' . t('Attention: token module is not enabled currently!');
  211. }
  212. $form['allowfullscreen'] = array(
  213. '#type' => 'checkbox',
  214. '#title' => t('Allow fullscreen'),
  215. '#default_value' => isset($settings['allowfullscreen'])? (int)$settings['allowfullscreen'] : 0,
  216. '#description' => t('Allow author to specify an additional class attribute for this iframe.'),
  217. );
  218. }
  219. return $form;
  220. }
  221. /**
  222. * Implements hook_field_validate().
  223. *
  224. * Possible error codes:
  225. * - 'widthheight': The value should be between 1 and 9999, or between 1% and 100%
  226. *
  227. * (entity_type: "node", entity: node-object, field: (field-settings + field-inst-settings) ..., items: current-items-of-page)
  228. */
  229. function iframe_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
  230. foreach ($items as $delta => $item) {
  231. if (isset($item['url']) && !empty($item['url'])) {
  232. if (isset($item['width']) && (empty($item['width']) || (int)$item['width']<1)) {
  233. $errors[$field['field_name']][$langcode][$delta][] = array(
  234. 'error' => '_iframe_widthsize_invalid',
  235. 'message' => t('%name: the value of width may be no less than 1.', array('%name' => t($instance['label']))),
  236. );
  237. }
  238. if (isset($item['height']) && (empty($item['height']) || $item['height']<1)) {
  239. $errors[$field['field_name']][$langcode][$delta][] = array(
  240. 'error' => '_iframe_heightsize_invalid',
  241. 'message' => t('%name: the value of height may be no less than 1.', array('%name' => t($instance['label']))),
  242. );
  243. }
  244. }
  245. }
  246. iframe_debug(1, 'iframe_field_validate errors', $errors);
  247. }
  248. /**
  249. * Implements hook_field_presave().
  250. *
  251. * We have to populate all missing default-values which were defined, but not edited by the current author
  252. * ($entity can be manipulated, because it shows only to the object)
  253. * (entity_type: "node", entity: node-object, field: (field-settings + field-inst-settings) ..., items: current-items-of-page)
  254. */
  255. function iframe_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
  256. $settings = $instance['settings'];
  257. $settings += $field['settings'];
  258. if ($field['type'] == 'iframe') {
  259. foreach ($items as $delta => $item) {
  260. if (isset($item['url'])) {
  261. // fill in missing attributes for saving
  262. $items[$delta] = $items[$delta] + $settings;
  263. }
  264. }
  265. iframe_debug(2, 'iframe_field_presave', array('entity_type'=>$entity_type, 'entity'=>$entity, 'field'=>$field, 'instance'=>$instance, 'items'=>$items));
  266. }
  267. }
  268. function iframe_field_attach_presave($entity_type, $entity) {
  269. iframe_debug(2, 'iframe_field_attach_presave', array('entity_type' => $entity_type, 'entity' => $entity));
  270. }
  271. /**
  272. * Implements hook_field_is_empty().
  273. */
  274. function iframe_field_is_empty($item, $field) {
  275. if ('iframe' == $field['type']) {
  276. if (empty($item['url'])) {
  277. return TRUE;
  278. }
  279. }
  280. return FALSE;
  281. }
  282. /***********************************************************************
  283. * Field Type API: Formatter
  284. **********************************************************************/
  285. /**
  286. * Implements hook_field_formatter_info().
  287. * used in field "manage display" for formatters
  288. * (Step 4)
  289. */
  290. function iframe_field_formatter_info() {
  291. return array(
  292. 'iframe_formatter_default' => array(
  293. 'label' => t('Title, over iframe (default)'),
  294. 'field types' => array('iframe'),
  295. ),
  296. 'iframe_formatter_only' => array(
  297. 'label' => t('Iframe without title'),
  298. 'field types' => array('iframe'),
  299. ),
  300. 'iframe_formatter_asurl' => array(
  301. 'label' => t('A link with the given title'),
  302. 'field types' => array('iframe'),
  303. ),
  304. 'iframe_formatter_asurlwithuri' => array(
  305. 'label' => t('A link with the uri as title'),
  306. 'field types' => array('iframe'),
  307. ),
  308. 'iframe_formatter_hidden' => array(
  309. 'label' => t('hidden - not shown (migration only)'),
  310. 'field types' => array('iframe'),
  311. ),
  312. );
  313. }
  314. /**
  315. * Implements hook_field_formatter_view().
  316. *
  317. * field settings = settings from "Feldtyp = field type" for the given generic field in the content-type
  318. * instance settings = generic settings from iframe-field (specific only in this content-type)
  319. * ==> take (1) field-settings => overwrite (if set) with (2) instance-settings => overwrite with (3) item-settings
  320. */
  321. function iframe_field_formatter_view($object_type, $object, $field, $instance, $langcode, $items, $display) {
  322. // object_type = node, object = Article-node-object
  323. iframe_debug(2, 'iframe_field_formatter_view', array('field' => $field, 'instance' => $instance, 'langcode' => $langcode, 'items' => '...', 'display' => $display));
  324. $element = array();
  325. $settings = $instance['settings'] + $field['settings']; // f-s rules i-s
  326. if (isset($settings['user_register_form'])) unset($settings['user_register_form']);
  327. # merge for sub-fields: "class"
  328. $settings['class'] = @trim($field['settings']['class'] . " " . $instance['settings']['class']);
  329. $class = $settings['class'];
  330. foreach ($items as $delta => $item) {
  331. $items[$delta]['delta'] = $delta;
  332. $items[$delta]['id'] = drupal_html_id($field['type'] . '-' . $field['field_name'] . '-' . $delta);
  333. $iclass = isset($item['class'])? $item['class'] : '';
  334. if (isset($settings['expose_class']) && $settings['expose_class']>0) {
  335. $items[$delta]['class'] = trim($class . " " . $iclass);
  336. }
  337. else {
  338. $items[$delta]['class'] = trim($class);
  339. }
  340. }
  341. if ($display['type']=='iframe_formatter_default') {
  342. foreach ($items as $delta => $item) {
  343. $element[$delta]['#markup'] = iframe_formatter_default_helper($item, $delta);
  344. }
  345. }
  346. elseif ($display['type']=='iframe_formatter_only') {
  347. foreach ($items as $delta => $item) {
  348. $element[$delta]['#markup'] = iframe_formatter_only_helper($item, $delta);
  349. }
  350. }
  351. elseif ($display['type']=='iframe_formatter_asurl') {
  352. foreach ($items as $delta => $item) {
  353. $element[$delta]['#markup'] = iframe_formatter_asurl_helper($item, $delta);
  354. }
  355. }
  356. elseif ($display['type']=='iframe_formatter_asurlwithuri') {
  357. foreach ($items as $delta => $item) {
  358. $element[$delta]['#markup'] = iframe_formatter_asurlwithuri_helper($item, $delta);
  359. }
  360. }
  361. elseif ($display['type']=='iframe_formatter_hidden') {
  362. foreach ($items as $delta => $item) {
  363. $element[$delta]['#markup'] = '';
  364. }
  365. }
  366. return $element;
  367. }
  368. /**
  369. * Formatter function for 'default' text field formatter.
  370. */
  371. function iframe_formatter_default_helper($item, $delta) {
  372. // If no url given display nothing.
  373. if (empty($item['url'])) {
  374. return '';
  375. }
  376. if (!isset($item['title'])) {
  377. $item['title'] = '';
  378. }
  379. // Display all
  380. return iframe_iframe($item['title'], $item['url'], $item);
  381. }
  382. /**
  383. * Formatter function for 'iframeonly' text field formatter.
  384. */
  385. function iframe_formatter_only_helper($item, $delta) {
  386. // If no url given display nothing.
  387. if (empty($item['url'])) {
  388. return '';
  389. }
  390. // Display all
  391. return iframe_iframe('', $item['url'], $item);
  392. }
  393. /**
  394. * Formatter function for 'asurl' text field formatter.
  395. */
  396. function iframe_formatter_asurl_helper($item, $delta) {
  397. // If no url given display nothing.
  398. if (empty($item['url'])) {
  399. return '';
  400. }
  401. // Display all
  402. $linktext = empty($item['title'])? $item['url'] : $item['title'];
  403. return l($linktext, $item['url'], $item);
  404. }
  405. /**
  406. * Formatter function for 'asurlwithuri' text field formatter.
  407. */
  408. function iframe_formatter_asurlwithuri_helper($item, $delta) {
  409. // If no url given display nothing.
  410. if (empty($item['url'])) {
  411. return '';
  412. }
  413. // Display all
  414. $linktext = $item['url'];
  415. return l($linktext, $item['url'], $item);
  416. }
  417. /**
  418. * Formatter function for 'hidden' text field formatter.
  419. */
  420. function iframe_formatter_hidden_helper($item, $delta) {
  421. return '';
  422. }
  423. /*
  424. * like central function
  425. * form the iframe code
  426. */
  427. function iframe_iframe($text, $path, $options = FALSE) {
  428. global $user, $language, $node;
  429. iframe_debug(1, 'iframe_iframe item', $options);
  430. if (!$options) {
  431. $options = array();
  432. }
  433. // Merge in defaults.
  434. $options += array(
  435. 'html' => FALSE,
  436. 'style' => '',
  437. );
  438. $allow = array();
  439. if (!isset($options['width'])) {
  440. $options['width'] = '100%';
  441. }
  442. if (!isset($options['height'])) {
  443. $options['height'] = '701';
  444. }
  445. if (isset($options['frameborder'])) {
  446. if ($options['frameborder'] > 0) {
  447. $options['style'] .= 'border-width:2px;';
  448. }
  449. else {
  450. $options['style'] .= 'border-width:0;';
  451. }
  452. unset($options['frameborder']);
  453. }
  454. if (isset($options['scrolling'])) {
  455. if ($options['scrolling'] == 'yes') {
  456. $options['style'] .= 'overflow:scroll;';
  457. }
  458. elseif ($options['scrolling'] == 'no') {
  459. $options['style'] .= 'overflow:hidden;';
  460. }
  461. else { # auto
  462. $options['style'] .= 'overflow:auto;';
  463. }
  464. unset($options['scrolling']);
  465. }
  466. if (isset($options['transparency'])) {
  467. if ($options['transparency'] > 0) {
  468. $options['style'] .= 'background-color:transparent;';
  469. }
  470. unset($options['transparency']);
  471. }
  472. if (!isset($options['tokensupport']) || $options['tokensupport'] < 0) {
  473. $options['tokensupport'] = 0;
  474. }
  475. if (isset($options['allowfullscreen']) && $options['allowfullscreen'] == 1) {
  476. $allow[] = 'fullscreen';
  477. }
  478. unset($options['allowfullscreen']);
  479. $allow[] = 'autoplay';
  480. $allow[] = 'camera';
  481. $allow[] = 'microphone';
  482. $allow[] = 'payment';
  483. $allow[] = 'accelerometer';
  484. $allow[] = 'geolocation';
  485. $allow[] = 'encrypted-media';
  486. $allow[] = 'gyroscope';
  487. $options['allow'] = implode(';', $allow);
  488. // Append active class.
  489. if ($path == $_GET['q'] || ($path == '<front>' && drupal_is_front_page())) {
  490. if (isset($options['class'])) {
  491. $options['class'] .= ' active';
  492. }
  493. else {
  494. $options['class'] = 'active';
  495. }
  496. }
  497. // Remove all HTML and PHP tags from a tooltip. For best performance, we act only
  498. // if a quick strpos() pre-check gave a suspicion (because strip_tags() is expensive).
  499. if (isset($options['title']) && strpos($options['title'], '<') !== FALSE) {
  500. $options['title'] = strip_tags($options['title']);
  501. }
  502. $options_link = array(); $options_link['attributes'] = array();
  503. $options_link['attributes']['title'] = $options['title'];
  504. if (module_exists('token')) {
  505. // Token Support for field "url" and "title"
  506. if (!isset($node)) {
  507. $node = menu_get_object();
  508. }
  509. $tokensupport = !empty($options['tokensupport'])? (int)$options['tokensupport'] : 0;
  510. if ($tokensupport > 0) {
  511. $text = token_replace($text, array('user' => $user, 'node' => $node), array('language' => $language)); // node:, user: are default token targets
  512. }
  513. if ($tokensupport > 1) {
  514. $path = token_replace($path, array('user' => $user, 'node' => $node), array('language' => $language));
  515. }
  516. }
  517. if (!isset($options['name']) && !empty($options['id'])) {
  518. $options['name'] = $options['id'];
  519. }
  520. $iframearguments_allowed = array('title', 'width', 'height', 'class', 'style', 'id', 'name', 'allow');
  521. $iframearguments = array();
  522. foreach($iframearguments_allowed as $argkey) {
  523. if (isset($options[$argkey])) {
  524. $iframearguments[$argkey] = $options[$argkey];
  525. }
  526. }
  527. $options['external'] = TRUE;
  528. return
  529. '<div class="' . (isset($options['class'])? check_plain($options['class']) : '') . '">'
  530. . (empty($text)? '' : '<h3 class="iframe_title">' . ($options['html'] ? $text : check_plain($text)) . '</h3>')
  531. . '<iframe src="' . check_url(url($path, $options)) . '"'
  532. . drupal_attributes($iframearguments)
  533. . '>'
  534. . t('Your browser does not support iframes, but you can use the following link.') . ' ' . l('Link', url($path, $options), $options_link)
  535. . '</iframe>'
  536. . '</div>'
  537. ;
  538. }
  539. /**************************************************************************
  540. * Field Type API: Widget
  541. **************************************************************************/
  542. /**
  543. * Implements hook_field_widget_info().
  544. * shows the "Widget" select for this special field type on the manage-fields page of content-type
  545. * The type of form element you would like to present to the user when creating this field in the test type.
  546. * (Step 1)
  547. */
  548. function iframe_field_widget_info() {
  549. return array(
  550. 'iframe_widget_url' => array(
  551. 'label' => t('URL only'),
  552. 'field types' => array('iframe'),
  553. 'settings' => array('size' => 255),
  554. 'weight' => 1,
  555. ),
  556. 'iframe_widget_urlheight' => array(
  557. 'label' => t('URL with height'),
  558. 'field types' => array('iframe'),
  559. 'settings' => array('size' => 255),
  560. 'weight' => 2,
  561. ),
  562. 'iframe_widget_urlwidthheight' => array(
  563. 'label' => t('URL with width and height'),
  564. 'field types' => array('iframe'),
  565. 'settings' => array('size' => 255),
  566. 'weight' => 3,
  567. ),
  568. );
  569. }
  570. /**
  571. * Implements hook_field_widget_settings_form().
  572. */
  573. function iframe_DISABLED_field_widget_settings_form($field, $instance) {
  574. $widget = $instance['widget'];
  575. $settings = $widget['settings'];
  576. iframe_debug(1, 'iframe_field_widget_settings_form', array('field' => $field, 'instance' => $instance));
  577. $form = array();
  578. /*
  579. $form['field_widget_settings_form'] = array(
  580. '#type' => 'textfield',
  581. '#title' => t('field_widget_settings_form'),
  582. '#default_value' => $settings['width'],
  583. '#required' => TRUE,
  584. //'#element_validate' => array('element_validate_integer_positive'),
  585. );
  586. */
  587. return $form;
  588. }
  589. /**
  590. * Implements hook_field_widget_form().
  591. * depends on the widget-type
  592. * Field XYZ sub-menu "Edit" (used in the middle for "default" values for each individual instance-item,
  593. * prefilled, with setting from iframe_field_info->instance_settings)
  594. * (Step 3)
  595. */
  596. function iframe_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  597. iframe_debug(2, 'iframe_field_widget_form', $form);
  598. $base = $element;
  599. $widget = $instance['widget'];
  600. #$settings = $widget['settings']; // size=255
  601. $settings = $instance['settings']; // for defaults
  602. // description of widget (help-text)
  603. switch ($instance['widget']['type']) {
  604. case 'iframe_widget_urlwidthheight':
  605. $element['#description'] = t('Set your iframe *URL* and the *width* and *height*.');
  606. break;
  607. case 'iframe_widget_urlheight':
  608. $element['#description'] = t('Set your iframe *URL* and the *height*.');
  609. break;
  610. case 'iframe_widget_url':
  611. default:
  612. $element['#description'] = t('Set your iframe *URL*.');
  613. break;
  614. }
  615. switch ($instance['widget']['type']) {
  616. case 'iframe_widget_urlwidthheight':
  617. $fwidth = (isset($items[$delta]) && isset($items[$delta]['width']))
  618. ? $items[$delta]['width']
  619. : (!empty($settings['width'])
  620. ? $settings['width']
  621. : NULL
  622. );
  623. $element['width'] = array(
  624. '#type' => 'textfield',
  625. '#title' => t('width'),
  626. '#default_value' => $fwidth,
  627. '#size' => 4,
  628. '#maxlength' => 4,
  629. '#weight' => 4,
  630. '#delta' => $delta,
  631. );
  632. // no "break" intentionally, fall through
  633. case 'iframe_widget_urlheight':
  634. $fheight = (isset($items[$delta]) && isset($items[$delta]['height']))
  635. ? $items[$delta]['height']
  636. : (!empty($settings['height'])
  637. ? $settings['height']
  638. : NULL
  639. );
  640. $element['height'] = array(
  641. '#type' => 'textfield',
  642. '#title' => t('height'),
  643. '#default_value' => $fheight,
  644. '#size' => 4,
  645. '#maxlength' => 4,
  646. '#weight' => 5,
  647. '#delta' => $delta,
  648. '#element_validate' => array('element_validate_integer_positive'),
  649. );
  650. // no "break" intentionally, fall through
  651. case 'iframe_widget_url':
  652. default:
  653. //
  654. break;
  655. }
  656. $title = (isset($items[$delta]) && isset($items[$delta]['title']))
  657. ? $items[$delta]['title']
  658. : NULL;
  659. $element['title'] = array(
  660. '#type' => 'textfield',
  661. '#title' => t('Iframe Title'),
  662. '#placeholder' => '',
  663. '#default_value' => $title,
  664. '#size' => 80,
  665. '#maxlength' => 1024,
  666. '#weight' => 2,
  667. '#delta' => $delta,
  668. //'#element_validate' => array('text'),
  669. );
  670. $furl = (isset($items[$delta]) && isset($items[$delta]['url']))
  671. ? $items[$delta]['url']
  672. : NULL;
  673. $element['url'] = array(
  674. '#type' => 'textfield',
  675. '#title' => t('Iframe URL'),
  676. '#placeholder' => 'http://',
  677. '#default_value' => $furl,
  678. '#size' => 80,
  679. '#maxlength' => 1024,
  680. '#weight' => 1,
  681. '#delta' => $delta,
  682. '#element_validate' => array('iframe_validate_url'),
  683. );
  684. $fclass = (isset($items[$delta]) && isset($items[$delta]['class']))
  685. ? $items[$delta]['class']
  686. : NULL;
  687. $expose_class = isset($settings['expose_class'])? $settings['expose_class'] : 1;
  688. if ($expose_class) {
  689. $element['class'] = array(
  690. '#type' => 'textfield',
  691. '#title' => t('Additional CSS Class'),
  692. '#default_value' => $fclass,
  693. '#description' => t('Multiple classes should be separated by spaces.<br />You can use a class "autoresize" if You want height-autoresizing for iframes of the same domain.'),
  694. '#size' => 80,
  695. '#weight' => 5,
  696. '#delta' => $delta,
  697. #'#element_validate' => array('iframe_validate_additionalclass'),
  698. );
  699. }
  700. // Display the list of available placeholders if token module is installed.
  701. $tokensupport = !empty($settings['tokensupport'])? (int)$settings['tokensupport'] : 0;
  702. if ($tokensupport > 0) {
  703. if (module_exists('token')) {
  704. $element['token_tree'] = array(
  705. '#theme' => 'token_tree_link',
  706. '#token_types' => array('node', 'user'),
  707. '#weight' => 3,
  708. );
  709. $element['url']['#description'] = (!empty($element['url']['#description'])? $element['url']['#description']." " : "")
  710. . t('Field does support tokens.');
  711. $element['title']['#description'] = (!empty($element['title']['#description'])? $element['title']['#description']." " : "")
  712. . t('Field does support tokens.');
  713. }
  714. else {
  715. $element['token_tree'] = array(
  716. '#markup' => '<p>' . t('Enable the <a href="@drupal-token">Token module</a> to view the available token browser.', array('@drupal-token' => 'http://drupal.org/project/token')) . '</p>',
  717. );
  718. }
  719. }
  720. iframe_debug(2, 'iframe_field_widget_form', array('settings' => $settings, 'instance-settings' => $instance['settings']));
  721. return $element;
  722. }
  723. /**
  724. * Implements hook_field_widget_error().
  725. */
  726. function iframe_DISABLED_field_widget_error($element, $error) {
  727. switch ($error['error']) {
  728. case 'widthsize':
  729. form_error($element['width'], $error['message']);
  730. break;
  731. case 'heightsize':
  732. form_error($element['height'], $error['message']);
  733. break;
  734. }
  735. }
  736. /**
  737. * Function to validate the iframe URL.
  738. */
  739. function iframe_validate_url($element, &$form_state) {
  740. if (!empty($element['#value'])) {
  741. if (preg_match('#(^http|\://)#', $element['#value'])) {
  742. if (!valid_url($element['#value'], TRUE)) {
  743. form_error($element, t('Invalid iframe URL.'));
  744. }
  745. }
  746. else { # not absolute url
  747. if (!valid_url($element['#value'], FALSE)) {
  748. form_error($element, t('Invalid iframe URL.'));
  749. }
  750. }
  751. }
  752. }
  753. /**
  754. * Support for local-domain iframes and autoresize the height
  755. *
  756. * Implementation of hook_init().
  757. */
  758. function iframe_init() {
  759. drupal_add_js(drupal_get_path('module', 'iframe') .'/iframe.js');
  760. if (!function_exists('iframe_content_migrate_field_alter')) {
  761. if (function_exists('module_load_include')) {
  762. module_load_include('inc', 'iframe', 'iframe.content_migrate.iframe');
  763. }
  764. }
  765. if (!function_exists('iframe_feeds_set_target')) {
  766. if (function_exists('module_load_include')) {
  767. module_load_include('inc', 'iframe', 'mappers/iframe');
  768. }
  769. }
  770. }