paragraphs.module 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368
  1. <?php
  2. /**
  3. * @file
  4. * Paragraphs hooks and common functions.
  5. * Paragraphs allows you to embed multiple entity bundles in field.
  6. */
  7. define('PARAGRAPHS_RECURSION_LIMIT', 20);
  8. define('PARAGRAPHS_DEFAULT_TITLE', 'Paragraph');
  9. define('PARAGRAPHS_DEFAULT_TITLE_MULTIPLE', 'Paragraphs');
  10. define('PARAGRAPHS_DEFAULT_EDIT_MODE', 'open');
  11. define('PARAGRAPHS_DEFAULT_ADD_MODE', 'select');
  12. /**
  13. * Modules should return this value from hook_paragraphs_item_access() to allow access to a paragraphs item.
  14. */
  15. define('PARAGRAPHS_ITEM_ACCESS_ALLOW', 'allow');
  16. /**
  17. * Modules should return this value from hook_paragraphs_item_access() to deny access to a paragraphs item.
  18. */
  19. define('PARAGRAPHS_ITEM_ACCESS_DENY', 'deny');
  20. /**
  21. * Modules should return this value from hook_paragraphs_item_access() to not affect paragraphs item access.
  22. */
  23. define('PARAGRAPHS_ITEM_ACCESS_IGNORE', NULL);
  24. // Separate some Field API parts in different files.
  25. require_once dirname(__FILE__) . '/paragraphs.field_formatter.inc';
  26. require_once dirname(__FILE__) . '/paragraphs.field_widget.inc';
  27. require_once dirname(__FILE__) . '/paragraphs.node_clone.inc';
  28. /**
  29. * Loads a paragraphs item.
  30. *
  31. * @param $item_id
  32. * The paragraphs item ID.
  33. * @param $reset
  34. * Should we reset the entity cache?
  35. * @return ParagraphsItemEntity
  36. * The paragraphs item entity or FALSE.
  37. */
  38. function paragraphs_item_load($item_id, $reset = FALSE) {
  39. $result = paragraphs_item_load_multiple(array($item_id), array(), $reset);
  40. return $result ? reset($result) : FALSE;
  41. }
  42. /**
  43. * Loads a paragraphs revision.
  44. *
  45. * @param $revision_id
  46. * The paragraphs revision ID.
  47. * @return ParagraphsItemEntity
  48. * The paragraphs item entity or FALSE.
  49. */
  50. function paragraphs_item_revision_load($revision_id) {
  51. return entity_revision_load('paragraphs_item', $revision_id);
  52. }
  53. /**
  54. * Loads paragraphs items.
  55. *
  56. * @param $ids
  57. * An array of paragraphs item IDs or FALSE to load all.
  58. * @param $conditions
  59. * Should we reset the entity cache?
  60. * @param $reset
  61. * Should we reset the entity cache?
  62. * @return ParagraphsItemEntity[]
  63. * An array of paragraphs item entities.
  64. */
  65. function paragraphs_item_load_multiple($ids = array(), $conditions = array(), $reset = FALSE) {
  66. return entity_load('paragraphs_item', $ids, $conditions, $reset);
  67. }
  68. /**
  69. * Implements hook_ctools_plugin_directory().
  70. */
  71. function paragraphs_ctools_plugin_directory($module, $plugin) {
  72. if ($module == 'panelizer' && $plugin == 'entity') {
  73. return 'plugins/panelizer/entity';
  74. }
  75. }
  76. /**
  77. * Implements hook_entity_info().
  78. */
  79. function paragraphs_entity_info() {
  80. $return['paragraphs_item'] = array(
  81. 'label' => t('Paragraphs item'),
  82. 'label callback' => 'entity_class_label',
  83. 'uri callback' => 'entity_class_uri',
  84. 'entity class' => 'ParagraphsItemEntity',
  85. 'controller class' => 'EntityAPIController',
  86. 'base table' => 'paragraphs_item',
  87. 'revision table' => 'paragraphs_item_revision',
  88. 'fieldable' => TRUE,
  89. // For integration with Redirect module.
  90. // @see http://drupal.org/node/1263884
  91. 'redirect' => FALSE,
  92. 'entity keys' => array(
  93. 'id' => 'item_id',
  94. 'revision' => 'revision_id',
  95. 'bundle' => 'bundle',
  96. 'field_name' => 'field_name',
  97. ),
  98. 'module' => 'paragraphs',
  99. 'view modes' => array(
  100. 'full' => array(
  101. 'label' => t('Full content'),
  102. 'custom settings' => FALSE,
  103. ),
  104. 'paragraphs_editor_preview' => array(
  105. 'label' => t('Paragraphs Editor Preview'),
  106. 'custom settings' => TRUE,
  107. ),
  108. ),
  109. 'bundle keys' => array(
  110. 'bundle' => 'bundle',
  111. ),
  112. 'access callback' => 'paragraphs_item_access',
  113. 'metadata controller class' => 'ParagraphsItemMetadataController',
  114. );
  115. $bundles = paragraphs_bundle_load();
  116. // Add info about the bundles. We do not use field_info_fields() but directly
  117. // use field_read_fields() as field_info_fields() requires built entity info
  118. // to work.
  119. foreach ($bundles as $machine_name => $bundle) {
  120. $return['paragraphs_item']['bundles'][$bundle->bundle] = array(
  121. 'label' => t('Paragraphs bundle @bundle', array('@bundle' => $bundle->bundle)),
  122. 'admin' => array(
  123. 'path' => 'admin/structure/paragraphs/%paragraphs_bundle',
  124. 'real path' => 'admin/structure/paragraphs/' . strtr($machine_name, array('_' => '-')),
  125. 'bundle argument' => 3,
  126. 'access arguments' => array('administer paragraphs bundles'),
  127. ),
  128. );
  129. }
  130. if (module_exists('entitycache')) {
  131. $return['paragraphs_item']['field cache'] = FALSE;
  132. $return['paragraphs_item']['entity cache'] = TRUE;
  133. }
  134. return $return;
  135. }
  136. /**
  137. * Access check for paragraphs.
  138. *
  139. * Most of the time the access callback is on the host entity.
  140. * In some cases you want specific access checks on paragraphs.
  141. * You can do this by implementing hook_paragraphs_item_access().
  142. *
  143. * @return bool
  144. * Whether the user has access to a paragraphs item.
  145. */
  146. function paragraphs_item_access($op, $entity, $account, $entity_type) {
  147. // If no user object is supplied, the access check is for the current user.
  148. if (empty($account)) {
  149. $account = $GLOBALS['user'];
  150. }
  151. $permissions = &drupal_static(__FUNCTION__, array());
  152. // If the $op was not one of the supported ones, we return access denied.
  153. if (!in_array($op, array('view', 'update', 'delete', 'create'), TRUE)) {
  154. return FALSE;
  155. }
  156. // When we have no entity, create a generic cid.
  157. if (empty($entity)) {
  158. $cid = 'all_entities:' . $op;
  159. }
  160. // When OP is create, or the entity is new, the bundle is the cache key.
  161. elseif ($op == 'create' || (isset($entity->is_new) && $entity->is_new)) {
  162. $cid = $entity->bundle;
  163. }
  164. // Else our cid is entity specific.
  165. else {
  166. $cid = $entity->item_id . '_' . $entity->revision_id;
  167. }
  168. // If we've already checked access for this bundle, user and op, return from
  169. // cache. Otherwise, we are optimistic and consider that the user can
  170. // view / update / delete or create a paragraph.
  171. if (isset($permissions[$account->uid][$cid][$op])) {
  172. return $permissions[$account->uid][$cid][$op];
  173. }
  174. // We grant access to the paragraph item if both of the following conditions are met:
  175. // - No modules say to deny access.
  176. // - At least one module says to grant access.
  177. // If no module specified either allow or deny, we always allow.
  178. $access = module_invoke_all('paragraphs_item_access', $entity, $op, $account);
  179. if (in_array(PARAGRAPHS_ITEM_ACCESS_DENY, $access, TRUE)) {
  180. $user_access_permission = FALSE;
  181. }
  182. elseif (in_array(PARAGRAPHS_ITEM_ACCESS_ALLOW, $access, TRUE)) {
  183. $user_access_permission = TRUE;
  184. } else {
  185. // Deny access by default.
  186. $user_access_permission = FALSE;
  187. }
  188. // Store the result of the permission in our matrix.
  189. $permissions[$account->uid][$cid][$op] = $user_access_permission;
  190. return $permissions[$account->uid][$cid][$op];
  191. }
  192. /**
  193. * Access check for paragraphs.
  194. *
  195. * Finds the parent entity and then checks for access on the parent entity.
  196. *
  197. * @param ParagraphsItemEntity $entity
  198. * The entity to check for.
  199. *
  200. * @param string $op
  201. * The operation to check for.
  202. *
  203. * @param $account
  204. * The account to check for.
  205. *
  206. * @return bool
  207. * Whether the user has access to a paragraphs item.
  208. */
  209. function paragraphs_paragraphs_item_access($entity, $op, $account) {
  210. $permissions = &drupal_static(__FUNCTION__, array());
  211. $parent_permissions = &drupal_static(__FUNCTION__ . '_parents', array());
  212. if (!in_array($op, array('view', 'update', 'delete', 'create'), TRUE)) {
  213. // If there was no bundle to check against, or the $op was not one of the
  214. // supported ones, we return access denied.
  215. return PARAGRAPHS_ITEM_ACCESS_IGNORE;
  216. }
  217. $check_parent_op = $op;
  218. // Update/Delete/Create access requires update access on the parent.
  219. if (in_array($op, array('update', 'delete', 'create'), TRUE)) {
  220. $check_parent_op = 'update';
  221. }
  222. // When we have no entity, create a generic cid.
  223. if (empty($entity)) {
  224. $cid = 'all_entities:' . $op;
  225. }
  226. // When OP is create, or the entity is new, the bundle is the cache key.
  227. elseif ($op == 'create' || (isset($entity->is_new) && $entity->is_new)) {
  228. $cid = $entity->bundle;
  229. }
  230. // Else our cid is entity specific.
  231. else {
  232. $cid = $entity->item_id . '_' . $entity->revision_id;
  233. }
  234. // Check if we cached permission check.
  235. if (isset($permissions[$account->uid][$cid][$op])) {
  236. return $permissions[$account->uid][$cid][$op];
  237. }
  238. if (empty($entity)) {
  239. // Ignore access when we don't have a host entity.
  240. $permissions[$account->uid][$cid][$op] = PARAGRAPHS_ITEM_ACCESS_IGNORE;
  241. } elseif ($host_entity = $entity->hostEntity()) {
  242. $host_entity_info = entity_get_info($entity->hostEntityType());
  243. $host_id_key = $host_entity_info['entity keys']['id'];
  244. $parent_cid = $entity->hostEntityType() . '_' . implode('_', entity_extract_ids($entity->hostEntityType(), $host_entity));
  245. // Check if we have an ID key set, if not parent entity is new, we check for create access.
  246. if (!isset($host_entity->{$host_id_key}) || empty($host_entity->{$host_id_key})) {
  247. $check_parent_op = 'create';
  248. }
  249. if (isset($parent_permissions[$account->uid][$parent_cid][$check_parent_op])) {
  250. return $parent_permissions[$account->uid][$parent_cid][$check_parent_op];
  251. }
  252. if (entity_access($check_parent_op, $entity->hostEntityType(), $host_entity)) {
  253. $permissions[$account->uid][$cid][$op] = PARAGRAPHS_ITEM_ACCESS_ALLOW;
  254. $parent_permissions[$account->uid][$parent_cid][$check_parent_op] = PARAGRAPHS_ITEM_ACCESS_ALLOW;
  255. } else {
  256. // Deny access as parent entity access failed.
  257. $permissions[$account->uid][$cid][$op] = PARAGRAPHS_ITEM_ACCESS_DENY;
  258. $parent_permissions[$account->uid][$parent_cid][$check_parent_op] = PARAGRAPHS_ITEM_ACCESS_DENY;
  259. }
  260. } else {
  261. // Ignore access when we don't have a host entity.
  262. $permissions[$account->uid][$cid][$op] = PARAGRAPHS_ITEM_ACCESS_IGNORE;
  263. }
  264. return $permissions[$account->uid][$cid][$op];
  265. }
  266. /**
  267. * Implements hook_permission().
  268. */
  269. function paragraphs_permission() {
  270. $perms = array(
  271. 'administer paragraphs bundles' => array(
  272. 'title' => t('Administer paragraphs bundles'),
  273. 'description' => t('Is able to administer paragraph bundles for the Paragraphs module'),
  274. ),
  275. );
  276. return $perms;
  277. }
  278. /**
  279. * Implements hook_menu().
  280. */
  281. function paragraphs_menu() {
  282. $items = array();
  283. $items['admin/structure/paragraphs'] = array(
  284. 'title' => 'Paragraph Bundles',
  285. 'description' => 'Manage Paragraph bundles',
  286. 'page callback' => 'paragraphs_admin_bundle_overview',
  287. 'access arguments' => array('administer paragraphs bundles'),
  288. 'file' => 'paragraphs.admin.inc',
  289. );
  290. $items['admin/structure/paragraphs/list'] = array(
  291. 'title' => 'List',
  292. 'type' => MENU_DEFAULT_LOCAL_TASK,
  293. 'weight' => -10,
  294. );
  295. $items['admin/structure/paragraphs/add'] = array(
  296. 'title' => 'Add Paragraph Bundle',
  297. 'page callback' => 'drupal_get_form',
  298. 'page arguments' => array('paragraphs_admin_bundle_form'),
  299. 'access arguments' => array('administer paragraphs bundles'),
  300. 'type' => MENU_LOCAL_ACTION,
  301. 'file' => 'paragraphs.admin.inc',
  302. );
  303. $items['admin/structure/paragraphs/%paragraphs_bundle'] = array(
  304. 'title' => 'Edit paragraph bundle',
  305. 'title callback' => 'paragraphs_bundle_title_callback',
  306. 'title arguments' => array(3),
  307. 'page callback' => 'drupal_get_form',
  308. 'page arguments' => array('paragraphs_admin_bundle_form', 3),
  309. 'access arguments' => array('administer paragraphs bundles'),
  310. 'file' => 'paragraphs.admin.inc',
  311. );
  312. $items['admin/structure/paragraphs/%paragraphs_bundle/edit'] = array(
  313. 'title' => 'Edit',
  314. 'type' => MENU_DEFAULT_LOCAL_TASK,
  315. );
  316. $items['admin/structure/paragraphs/%paragraphs_bundle/delete'] = array(
  317. 'title' => 'Delete Paragraph Bundle',
  318. 'page callback' => 'drupal_get_form',
  319. 'page arguments' => array('paragraphs_admin_bundle_delete_form', 3),
  320. 'access arguments' => array('administer paragraphs bundles'),
  321. 'file' => 'paragraphs.admin.inc',
  322. );
  323. $items['paragraphs/edit/ajax'] = array(
  324. 'title' => 'Edit item callback',
  325. 'page callback' => 'paragraphs_edit_js',
  326. 'delivery callback' => 'ajax_deliver',
  327. 'access callback' => TRUE,
  328. 'theme callback' => 'ajax_base_page_theme',
  329. 'type' => MENU_CALLBACK,
  330. 'file' => 'paragraphs.ajax.inc',
  331. );
  332. $items['paragraphs/collapse/ajax'] = array(
  333. 'title' => 'Close item callback',
  334. 'page callback' => 'paragraphs_collapse_js',
  335. 'delivery callback' => 'ajax_deliver',
  336. 'access callback' => TRUE,
  337. 'theme callback' => 'ajax_base_page_theme',
  338. 'type' => MENU_CALLBACK,
  339. 'file' => 'paragraphs.ajax.inc',
  340. );
  341. $items['paragraphs/remove/ajax'] = array(
  342. 'title' => 'Remove item callback',
  343. 'page callback' => 'paragraphs_remove_js',
  344. 'delivery callback' => 'ajax_deliver',
  345. 'access callback' => TRUE,
  346. 'theme callback' => 'ajax_base_page_theme',
  347. 'type' => MENU_CALLBACK,
  348. 'file' => 'paragraphs.ajax.inc',
  349. );
  350. $items['paragraphs/deleteconfirm/ajax'] = array(
  351. 'title' => 'Remove item callback',
  352. 'page callback' => 'paragraphs_deleteconfirm_js',
  353. 'delivery callback' => 'ajax_deliver',
  354. 'access callback' => TRUE,
  355. 'theme callback' => 'ajax_base_page_theme',
  356. 'type' => MENU_CALLBACK,
  357. 'file' => 'paragraphs.ajax.inc',
  358. );
  359. $items['paragraphs/restore/ajax'] = array(
  360. 'title' => 'Restore item callback',
  361. 'page callback' => 'paragraphs_restore_js',
  362. 'delivery callback' => 'ajax_deliver',
  363. 'access callback' => TRUE,
  364. 'theme callback' => 'ajax_base_page_theme',
  365. 'type' => MENU_CALLBACK,
  366. 'file' => 'paragraphs.ajax.inc',
  367. );
  368. return $items;
  369. }
  370. /**
  371. * Implements hook_field_info().
  372. */
  373. function paragraphs_field_info() {
  374. $info = array();
  375. $info['paragraphs'] = array(
  376. 'label' => t('Paragraphs'),
  377. 'description' => t('Paragraphs field using the paragraph bundles.'),
  378. 'instance_settings' => array(
  379. 'title' => PARAGRAPHS_DEFAULT_TITLE,
  380. 'title_multiple' => PARAGRAPHS_DEFAULT_TITLE_MULTIPLE,
  381. 'allowed_bundles' => array(),
  382. 'bundle_weights' => array(),
  383. ),
  384. 'default_widget' => 'paragraphs_hidden',
  385. 'default_formatter' => 'paragraphs_view',
  386. 'settings' => array(),
  387. 'property_type' => 'paragraphs_item',
  388. 'property_callbacks' => array('paragraphs_entity_metadata_property_callback'),
  389. );
  390. return $info;
  391. }
  392. /**
  393. * Implements hook_form_field_ui_field_edit_form_alter().
  394. */
  395. function paragraphs_form_field_ui_field_edit_form_alter(&$form, $form_state) {
  396. if ($form['#field']['type'] == 'paragraphs') {
  397. $form['#theme'] = array('paragraphs_bundle_settings_form');
  398. array_unshift($form['#submit'], 'paragraphs_bundle_settings_form_submit');
  399. }
  400. }
  401. function paragraphs_bundle_settings_form_submit($form, &$form_state) {
  402. $bundle_settings = array();
  403. $bundle_weights = array();
  404. if (isset($form_state['values']['instance']['settings']['allowed_bundles_table'])) {
  405. $bundle_settings_table = $form_state['values']['instance']['settings']['allowed_bundles_table'];
  406. uasort($bundle_settings_table, 'drupal_sort_weight');
  407. foreach ($bundle_settings_table as $machine_name => $value) {
  408. $bundle_settings[$machine_name] = (($value['enabled'] === 1) ? $machine_name : -1);
  409. $bundle_weights[$machine_name] = $value['weight'];
  410. }
  411. }
  412. $form_state['values']['instance']['settings']['allowed_bundles'] = $bundle_settings;
  413. $form_state['values']['instance']['settings']['bundle_weights'] = $bundle_weights;
  414. unset($form_state['values']['instance']['settings']['allowed_bundles_table']);
  415. }
  416. /**
  417. * Implements hook_field_instance_settings_form().
  418. */
  419. function paragraphs_field_instance_settings_form($field, $instance) {
  420. $settings = $instance['settings'];
  421. $bundles = array();
  422. $_bundles = paragraphs_bundle_load();
  423. $form_delta = count($_bundles) * 2;
  424. $max_weight = 0;
  425. $weights = array();
  426. foreach ($_bundles as $machine_name => $bundle) {
  427. $bundles[$machine_name] = $bundle->name;
  428. if (isset($settings['bundle_weights'][$machine_name])) {
  429. $weights[$machine_name] = $settings['bundle_weights'][$machine_name];
  430. if ($settings['bundle_weights'][$machine_name] > $max_weight) {
  431. $max_weight = $settings['bundle_weights'][$machine_name];
  432. }
  433. }
  434. }
  435. $max_weight++;
  436. $element['allowed_bundles_table'] = array(
  437. '#tree' => TRUE,
  438. '#prefix' => '<label>' . t('Allowed Paragraph bundles') . '</label>',
  439. '#suffix' => '<div class="description">' . t('If no bundle is selected, all the bundles will be available.') . '</div>',
  440. );
  441. $weight = 1;
  442. foreach ($_bundles as $machine_name => $bundle) {
  443. $enabled = FALSE;
  444. if (isset($settings['allowed_bundles'][$machine_name]) && $settings['allowed_bundles'][$machine_name] === $machine_name) {
  445. $enabled = TRUE;
  446. }
  447. $element['allowed_bundles_table'][$machine_name] = array(
  448. 'enabled' => array(
  449. '#type' => 'checkbox',
  450. '#title' => check_plain($bundle->name),
  451. '#title_display' => 'after',
  452. '#default_value' => $enabled,
  453. ),
  454. 'weight' => array(
  455. '#type' => 'weight',
  456. '#title' => t('Weight'),
  457. '#default_value' => (isset($weights[$machine_name]) ? $weights[$machine_name] : $weight + $max_weight),
  458. '#delta' => $form_delta,
  459. '#title_display' => 'invisible',
  460. ),
  461. );
  462. $element['allowed_bundles_table'][$machine_name]['#weight'] = $element['allowed_bundles_table'][$machine_name]['weight']['#default_value'];
  463. $weight++;
  464. }
  465. $element['title'] = array(
  466. '#type' => 'textfield',
  467. '#title' => t('Item Title'),
  468. '#description' => t('Label to appear as title on the button as "Add new [title]", this label is translatable'),
  469. '#default_value' => isset($settings['title']) ? $settings['title'] : PARAGRAPHS_DEFAULT_TITLE,
  470. '#required' => TRUE,
  471. );
  472. $element['title_multiple'] = array(
  473. '#type' => 'textfield',
  474. '#title' => t('Plural Item Title'),
  475. '#description' => t('Title in its plural form.'),
  476. '#default_value' => isset($settings['title_multiple']) ? $settings['title_multiple'] : PARAGRAPHS_DEFAULT_TITLE_MULTIPLE,
  477. '#required' => TRUE,
  478. );
  479. $element['default_edit_mode'] = array(
  480. '#type' => 'select',
  481. '#title' => t('Default edit mode'),
  482. '#description' => t('The default edit mode the paragraph item is in. Preview will render the paragraph in the preview view mode.'),
  483. '#options' => array(
  484. 'open' => t('Open'),
  485. 'closed' => t('Closed'),
  486. 'preview' => t('Preview'),
  487. ),
  488. '#default_value' => isset($settings['default_edit_mode']) ? $settings['default_edit_mode'] : PARAGRAPHS_DEFAULT_EDIT_MODE,
  489. '#required' => TRUE,
  490. );
  491. $element['add_mode'] = array(
  492. '#type' => 'select',
  493. '#title' => t('Add mode'),
  494. '#description' => t('The way to add new paragraphs.'),
  495. '#options' => array(
  496. 'select' => t('Select List'),
  497. 'button' => t('Buttons'),
  498. ),
  499. '#default_value' => isset($settings['add_mode']) ? $settings['add_mode'] : PARAGRAPHS_DEFAULT_ADD_MODE,
  500. '#required' => TRUE,
  501. );
  502. if (!count($bundles)) {
  503. $element['allowed_bundles_explain'] = array(
  504. '#type' => 'markup',
  505. '#markup' => t('You did not add any paragraph bundles yet, click !here to add one.', array('!here' => l(t('here'), 'admin/structure/paragraphs/add', array('query' => drupal_get_destination()))))
  506. );
  507. }
  508. $element['fieldset'] = array(
  509. '#type' => 'fieldset',
  510. '#title' => t('Default value'),
  511. '#collapsible' => FALSE,
  512. // As field_ui_default_value_widget() does, we change the #parents so that
  513. // the value below is writing to $instance in the right location.
  514. '#parents' => array('instance'),
  515. );
  516. // Be sure to set the default value to NULL, for example to repair old fields
  517. // that still have one.
  518. $element['fieldset']['default_value'] = array(
  519. '#type' => 'value',
  520. '#value' => NULL,
  521. );
  522. $element['fieldset']['content'] = array(
  523. '#pre' => '<p>',
  524. '#markup' => t('To specify a default value, configure it via the regular default value setting of each field that is part of the paragraph bundle. To do so, go to the <a href="!url">Manage fields</a> screen of the paragraph bundle.', array('!url' => url('admin/structure/paragraphs'))),
  525. '#suffix' => '</p>',
  526. );
  527. return $element;
  528. }
  529. function theme_paragraphs_bundle_settings_form($variables) {
  530. $form = $variables['form'];
  531. // Initialize the variable which will store our table rows.
  532. $rows = array();
  533. uasort($form['instance']['settings']['allowed_bundles_table'], 'element_sort');
  534. // Iterate over each element in our $form['example_items'] array.
  535. foreach (element_children($form['instance']['settings']['allowed_bundles_table']) as $id) {
  536. // Before we add our 'weight' column to the row, we need to give the
  537. // element a custom class so that it can be identified in the
  538. // drupal_add_tabledrag call.
  539. //
  540. // This could also have been done during the form declaration by adding
  541. // '#attributes' => array('class' => 'example-item-weight'),
  542. // directy to the 'weight' element in tabledrag_example_simple_form().
  543. $form['instance']['settings']['allowed_bundles_table'][$id]['weight']['#attributes']['class'] = array('paragraphs-bundle-item-weight');
  544. // We are now ready to add each element of our $form data to the $rows
  545. // array, so that they end up as individual table cells when rendered
  546. // in the final table. We run each element through the drupal_render()
  547. // function to generate the final html markup for that element.
  548. $rows[] = array(
  549. 'data' => array(
  550. // Add our 'enabled' column.
  551. drupal_render($form['instance']['settings']['allowed_bundles_table'][$id]['enabled']),
  552. // Add our 'weight' column.
  553. drupal_render($form['instance']['settings']['allowed_bundles_table'][$id]['weight']),
  554. ),
  555. // To support the tabledrag behaviour, we need to assign each row of the
  556. // table a class attribute of 'draggable'. This will add the 'draggable'
  557. // class to the <tr> element for that row when the final table is
  558. // rendered.
  559. 'class' => array('draggable'),
  560. );
  561. }
  562. // We now define the table header values. Ensure that the 'header' count
  563. // matches the final column count for your table.
  564. $header = array(t('Bundle'), t('Weight'));
  565. // We also need to pass the drupal_add_tabledrag() function an id which will
  566. // be used to identify the <table> element containing our tabledrag form.
  567. // Because an element's 'id' should be unique on a page, make sure the value
  568. // you select is NOT the same as the form ID used in your form declaration.
  569. $table_id = drupal_html_id('paragraphs-bundle-table');
  570. // We can render our tabledrag table for output.
  571. $output = theme('table', array(
  572. 'header' => $header,
  573. 'rows' => $rows,
  574. 'attributes' => array('id' => $table_id),
  575. ));
  576. $form['instance']['settings']['allowed_bundles_table']['#markup'] = $output;
  577. // And then render any remaining form elements (such as our submit button).
  578. $output = drupal_render_children($form);
  579. // We now call the drupal_add_tabledrag() function in order to add the
  580. // tabledrag.js goodness onto our page.
  581. //
  582. // For a basic sortable table, we need to pass it:
  583. // - the $table_id of our <table> element,
  584. // - the $action to be performed on our form items ('order'),
  585. // - a string describing where $action should be applied ('siblings'),
  586. // - and the class of the element containing our 'weight' element.
  587. drupal_add_tabledrag($table_id, 'order', 'sibling', 'paragraphs-bundle-item-weight');
  588. return $output;
  589. }
  590. /**
  591. * Implements hook_field_settings_form().
  592. */
  593. function paragraphs_field_settings_form($field, $instance) {
  594. $form = array();
  595. return $form;
  596. }
  597. /**
  598. * Implements hook_field_presave().
  599. *
  600. * Support saving paragraph items in @code $item['entity'] @endcode. This
  601. * may be used to seamlessly create paragraph items during host-entity
  602. * creation or to save changes to the host entity and its collections at once.
  603. */
  604. function paragraphs_field_presave($host_entity_type, $host_entity, $field, $instance, $langcode, &$items) {
  605. foreach ($items as $key => &$item) {
  606. // In case the entity has been changed / created, save it and set the id.
  607. // If the host entity creates a new revision, save new item-revisions as
  608. // well.
  609. $entity = FALSE;
  610. if (isset($item['entity'])) {
  611. $entity = paragraphs_field_get_entity($item);
  612. } elseif (isset($item['revision_id'])) {
  613. $entity = paragraphs_item_revision_load($item['revision_id']);
  614. }
  615. if ($entity) {
  616. $entity->setHostEntity($host_entity_type, $host_entity, $langcode, FALSE);
  617. // If the host entity supports revisions and is saved as new revision, do the same for the item.
  618. if (!empty($host_entity->revision)) {
  619. $entity->revision = TRUE;
  620. $is_default = entity_revision_is_default($host_entity_type, $host_entity);
  621. // If an entity type does not support saving non-default entities,
  622. // assume it will be saved as default.
  623. if (!isset($is_default) || $is_default) {
  624. $entity->default_revision = TRUE;
  625. $entity->archived = FALSE;
  626. }
  627. }
  628. if (isset($entity->removed) && $entity->removed) {
  629. unset($items[$key]);
  630. }
  631. else {
  632. $entity->save(TRUE);
  633. $item = array(
  634. 'value' => $entity->item_id,
  635. 'revision_id' => $entity->revision_id,
  636. );
  637. }
  638. }
  639. }
  640. }
  641. /**
  642. * Implements hook_field_update().
  643. *
  644. * Care about removed paragraph items.
  645. */
  646. function paragraphs_field_update($entity_type, $entity, $field, $instance, $langcode, &$items) {
  647. // Prevent workbench moderation from deleting paragraphs on node_save() during
  648. // workbench_moderation_store(), when $host_entity->revision == 0.
  649. if (!empty($entity->workbench_moderation['updating_live_revision'])) {
  650. return;
  651. }
  652. $items_original = !empty($entity->original->{$field['field_name']}[$langcode]) ? $entity->original->{$field['field_name']}[$langcode] : array();
  653. $original_by_id = array_flip(paragraphs_field_item_to_ids($items_original));
  654. foreach ($items as $item) {
  655. unset($original_by_id[$item['value']]);
  656. }
  657. // If there are removed items, care about deleting the item entities.
  658. if ($original_by_id) {
  659. $ids = array_flip($original_by_id);
  660. // If we are creating a new revision, the old-items should be kept but get
  661. // marked as archived now.
  662. if (!empty($entity->revision)) {
  663. db_update('paragraphs_item')
  664. ->fields(array('archived' => 1))
  665. ->condition('item_id', $ids, 'IN')
  666. ->execute();
  667. }
  668. else {
  669. // Delete unused paragraph items now.
  670. foreach (paragraphs_item_load_multiple($ids) as $item) {
  671. $item->setHostEntity($entity_type, $entity, $langcode, FALSE);
  672. $item->deleteRevision(TRUE);
  673. }
  674. }
  675. }
  676. }
  677. /**
  678. * Implements hook_field_delete().
  679. */
  680. function paragraphs_field_delete($entity_type, $entity, $field, $instance, $langcode, &$items) {
  681. if ($field['type'] == 'paragraphs') {
  682. // Also delete all embedded entities.
  683. if ($ids = paragraphs_field_item_to_ids($items)) {
  684. // We filter out entities that are still being referenced by other
  685. // host-entities. This should never be the case, but it might happened e.g.
  686. // when modules cloned a node without knowing about paragraphs.
  687. $entity_info = entity_get_info($entity_type);
  688. $entity_id_name = $entity_info['entity keys']['id'];
  689. $field_column = key($field['columns']);
  690. // Extra check to make sure our field exists.
  691. if (is_scalar($field)) {
  692. $field_definition = field_info_field($field['field_name']);
  693. if (!empty($field_definition)) {
  694. foreach ($ids as $id_key => $id) {
  695. $query = new EntityFieldQuery();
  696. $entities = $query
  697. ->fieldCondition($field['field_name'], $field_column, $id)
  698. ->execute();
  699. unset($entities[$entity_type][$entity->$entity_id_name]);
  700. if (!empty($entities[$entity_type])) {
  701. // Filter this $id out.
  702. unset($ids[$id_key]);
  703. }
  704. }
  705. }
  706. }
  707. entity_delete_multiple('paragraphs_item', $ids);
  708. }
  709. }
  710. }
  711. /**
  712. * Implements hook_field_delete_revision().
  713. */
  714. function paragraphs_field_delete_revision($entity_type, $entity, $field, $instance, $langcode, &$items) {
  715. if ($field['type'] == 'paragraphs') {
  716. foreach ($items as $item) {
  717. if (!empty($item['revision_id'])) {
  718. if ($paragraphs_item = paragraphs_item_revision_load($item['revision_id'])) {
  719. $paragraphs_item->setHostEntity($entity_type, $entity, $langcode, FALSE);
  720. $paragraphs_item->deleteRevision(TRUE);
  721. }
  722. }
  723. }
  724. }
  725. }
  726. /**
  727. * Get an array of paragraph item IDs stored in the given field items.
  728. */
  729. function paragraphs_field_item_to_ids($items) {
  730. $ids = array();
  731. foreach ($items as $item) {
  732. if (!empty($item['value'])) {
  733. $ids[] = $item['value'];
  734. }
  735. }
  736. return $ids;
  737. }
  738. /**
  739. * Implements hook_field_is_empty().
  740. */
  741. function paragraphs_field_is_empty($item, $field) {
  742. // Item is empty when we removed it.
  743. if (isset($item['entity']) && ((isset($item['entity']->removed) && $item['entity']->removed) || (isset($item['entity']->confirmed_removed) && $item['entity']->confirmed_removed))) {
  744. return TRUE;
  745. }
  746. if (!empty($item['value'])) {
  747. return FALSE;
  748. }
  749. elseif (isset($item['entity'])) {
  750. return FALSE;
  751. }
  752. return TRUE;
  753. }
  754. /**
  755. * Determines whether a field paragraphs item entity is empty based on the paragraphs-field.
  756. */
  757. function paragraphs_item_is_empty(ParagraphsItemEntity $item) {
  758. $instances = field_info_instances('paragraphs_item', $item->bundle);
  759. $is_empty = TRUE;
  760. foreach ($instances as $instance) {
  761. $field_name = $instance['field_name'];
  762. $field = field_info_field($field_name);
  763. // Determine the list of languages to iterate on.
  764. $languages = field_available_languages('paragraphs_item', $field);
  765. foreach ($languages as $langcode) {
  766. if (!empty($item->{$field_name}[$langcode])) {
  767. // If at least one paragraph is not empty; the
  768. // paragraph item is not empty.
  769. foreach ($item->{$field_name}[$langcode] as $field_item) {
  770. if (!module_invoke($field['module'], 'field_is_empty', $field_item, $field)) {
  771. $is_empty = FALSE;
  772. }
  773. }
  774. }
  775. }
  776. }
  777. // Allow other modules a chance to alter the value before returning.
  778. drupal_alter('paragraphs_is_empty', $is_empty, $item);
  779. return $is_empty;
  780. }
  781. /**
  782. * Load a specific bundle or a list of bundles
  783. *
  784. * @param string|null $name
  785. * The machine name or list of bundles to load when null.
  786. *
  787. * @param bool $rebuild
  788. * Whether to use cache or not.
  789. *
  790. * @return array|stdClass|bool
  791. * The bundle, a list of bundles, or FALSE when not found.
  792. */
  793. function paragraphs_bundle_load($name = NULL, $rebuild = FALSE) {
  794. $cid = 'paragraphs_bundles';
  795. $bundles = array();
  796. // Load bundles from static or from Drupal cache
  797. $_bundles = &drupal_static($cid);
  798. if (isset($_bundles) && !$rebuild) {
  799. $bundles = $_bundles;
  800. }
  801. else {
  802. $_bundles = cache_get($cid);
  803. if ($_bundles && !$rebuild) {
  804. $bundles = $_bundles->data;
  805. }
  806. else {
  807. $query = db_select('paragraphs_bundle', 'pb')
  808. ->fields('pb')
  809. ->orderBy('pb.bundle', 'ASC');
  810. foreach ($query->execute() as $bundle_object) {
  811. $bundles[$bundle_object->bundle] = $bundle_object;
  812. }
  813. cache_set($cid, $bundles);
  814. }
  815. $_bundles = $bundles;
  816. }
  817. if ($name) {
  818. $name = strtr($name, array('-' => '_'));
  819. if (isset($bundles[$name])) {
  820. return $bundles[$name];
  821. }
  822. return FALSE;
  823. }
  824. else {
  825. return $bundles;
  826. }
  827. }
  828. /**
  829. * Menu load callback to scrub a paragraphs bundle from the URL safe equivalent.
  830. */
  831. function paragraphs_panelizer_bundle_name_load($name) {
  832. if (($bundle = paragraphs_bundle_load($name))) {
  833. return $bundle->bundle;
  834. }
  835. }
  836. /**
  837. * Function to create or update an paragraphs bundle.
  838. *
  839. * @param stdClass $bundle
  840. * The object of the bundle to create/update.
  841. *
  842. * @return int
  843. * SAVED_UPDATED when updated, SAVED_NEW when created.
  844. *
  845. * @throws Exception
  846. */
  847. function paragraphs_bundle_save($bundle) {
  848. $is_existing = (bool) db_query_range('SELECT 1 FROM {paragraphs_bundle} WHERE bundle = :bundle', 0, 1, array(':bundle' => $bundle->bundle))->fetchField();
  849. $fields = array(
  850. 'bundle' => (string) $bundle->bundle,
  851. 'name' => (string) $bundle->name,
  852. 'locked' => (int) $bundle->locked,
  853. );
  854. if ($is_existing) {
  855. db_update('paragraphs_bundle')
  856. ->fields($fields)
  857. ->condition('bundle', $bundle->bundle)
  858. ->execute();
  859. $status = SAVED_UPDATED;
  860. }
  861. else {
  862. db_insert('paragraphs_bundle')
  863. ->fields($fields)
  864. ->execute();
  865. $status = SAVED_NEW;
  866. }
  867. paragraphs_bundle_load(NULL, TRUE);
  868. entity_info_cache_clear();
  869. variable_set('menu_rebuild_needed', TRUE);
  870. return $status;
  871. }
  872. /**
  873. * Function to delete a bundle.
  874. *
  875. * @param $bundle_machine_name
  876. * Machine name of the bundle to delete.
  877. */
  878. function paragraphs_bundle_delete($bundle_machine_name) {
  879. $bundle = paragraphs_bundle_load($bundle_machine_name);
  880. if ($bundle) {
  881. db_delete('paragraphs_bundle')
  882. ->condition('bundle', $bundle->bundle)
  883. ->execute();
  884. field_attach_delete_bundle('paragraphs_item', $bundle->bundle);
  885. paragraphs_bundle_load(NULL, TRUE);
  886. entity_info_cache_clear();
  887. variable_set('menu_rebuild_needed', TRUE);
  888. }
  889. }
  890. /**
  891. * Entity property info setter callback for the host entity property.
  892. *
  893. * As the property is of type entity, the value will be passed as a wrapped
  894. * entity.
  895. */
  896. function paragraphs_item_set_host_entity($item, $property_name, $wrapper) {
  897. if (empty($item->is_new)) {
  898. throw new EntityMetadataWrapperException('The host entity may be set only during creation of a paragraphs item.');
  899. }
  900. $item->setHostEntity($wrapper->type(), $wrapper->value());
  901. }
  902. /**
  903. * Entity property info getter callback for the host entity property.
  904. */
  905. function paragraphs_item_get_host_entity($item) {
  906. // As the property is defined as 'entity', we have to return a wrapped entity.
  907. return entity_metadata_wrapper($item->hostEntityType(), $item->hostEntity());
  908. }
  909. /**
  910. * Callback for generating entity metadata property info for our field instances.
  911. *
  912. * @see paragraphs_field_info()
  913. */
  914. function paragraphs_entity_metadata_property_callback(&$info, $entity_type, $field, $instance, $field_type) {
  915. $property = &$info[$entity_type]['bundles'][$instance['bundle']]['properties'][$field['field_name']];
  916. $property['field_name'] = $field['field_name'];
  917. $property['getter callback'] = 'paragraphs_field_property_get';
  918. }
  919. /**
  920. * Entity property info getter callback for the paragraph items.
  921. *
  922. * Like entity_metadata_field_property_get(), but additionally supports getting
  923. * not-yet saved collection items from @code $item['entity'] @endcode.
  924. */
  925. function paragraphs_field_property_get($entity, array $options, $name, $entity_type, $info) {
  926. $field = field_info_field($name);
  927. $langcode = field_language($entity_type, $entity, $name, isset($options['language']) ? $options['language']->language : NULL);
  928. $values = array();
  929. if (isset($entity->{$name}[$langcode])) {
  930. foreach ($entity->{$name}[$langcode] as $delta => $data) {
  931. // Wrappers do not support multiple entity references being revisions or
  932. // not yet saved entities. In the case of a single reference we can return
  933. // the entity object though.
  934. if ($field['cardinality'] == 1) {
  935. $values[$delta] = paragraphs_field_get_entity($data);
  936. }
  937. elseif (isset($data['value'])) {
  938. $values[$delta] = $data['value'];
  939. }
  940. }
  941. }
  942. // For an empty single-valued field, we have to return NULL.
  943. return $field['cardinality'] == 1 ? ($values ? reset($values) : NULL) : $values;
  944. }
  945. /**
  946. * Gets a paragraphs item entity for a given field item.
  947. *
  948. * @param $field_name
  949. * (optional) If given and there is no entity yet, a new entity object is
  950. * created for the given item.
  951. *
  952. * @return
  953. * The entity object or FALSE.
  954. */
  955. function paragraphs_field_get_entity(&$item, $bundle = NULL, $field_name = NULL) {
  956. if (isset($item['entity'])) {
  957. return $item['entity'];
  958. }
  959. elseif (isset($item['value'])) {
  960. // By default always load the default revision, so caches get used.
  961. $entity = paragraphs_item_load($item['value']);
  962. if ($entity && $entity->revision_id != $item['revision_id']) {
  963. // A non-default revision is a referenced, so load this one.
  964. $entity = paragraphs_item_revision_load($item['revision_id']);
  965. }
  966. return $entity;
  967. }
  968. elseif (!isset($item['entity']) && isset($bundle) && isset($field_name)) {
  969. $item['entity'] = entity_create('paragraphs_item', array('bundle' => $bundle, 'field_name' => $field_name));
  970. return $item['entity'];
  971. }
  972. return FALSE;
  973. }
  974. /**
  975. * Returns HTML for an individual form element.
  976. *
  977. * Combine multiple values into a table with drag-n-drop reordering.
  978. * TODO : convert to a template.
  979. *
  980. * @param $variables
  981. * An associative array containing:
  982. * - element: A render element representing the form element.
  983. *
  984. * @ingroup themeable
  985. */
  986. function theme_paragraphs_field_multiple_value_form($variables) {
  987. $element = $variables['element'];
  988. $output = '';
  989. $instance = $element['#instance'];
  990. if (!isset($instance['settings']['title'])) {
  991. $instance['settings']['title'] = PARAGRAPHS_DEFAULT_TITLE;
  992. }
  993. if (!isset($instance['settings']['title_multiple'])) {
  994. $instance['settings']['title_multiple'] = PARAGRAPHS_DEFAULT_TITLE_MULTIPLE;
  995. }
  996. $add_mode = (isset($instance['settings']['add_mode']) ? $instance['settings']['add_mode'] : PARAGRAPHS_DEFAULT_ADD_MODE);
  997. $table_id = drupal_html_id($element['#field_name'] . '_values');
  998. $order_class = $element['#field_name'] . '-delta-order';
  999. $required = !empty($element['#required']) ? theme('form_required_marker', $variables) : '';
  1000. $header = array(
  1001. array(
  1002. 'data' => '<label>' . t('!title !required', array('!title' => $element['#title'], '!required' => $required)) . "</label>",
  1003. 'colspan' => 2,
  1004. 'class' => array('field-label'),
  1005. ),
  1006. t('Order'),
  1007. );
  1008. $rows = array();
  1009. // Sort items according to '_weight' (needed when the form comes back after
  1010. // preview or failed validation)
  1011. $items = array();
  1012. foreach (element_children($element) as $key) {
  1013. if ($key === 'add_more') {
  1014. $add_more_button = &$element[$key];
  1015. }
  1016. elseif ($key === 'add_more_type') {
  1017. $add_more_button_type = &$element[$key];
  1018. }
  1019. else {
  1020. if (!isset($element[$key]['#access']) || $element[$key]['#access']) {
  1021. $items[] = &$element[$key];
  1022. }
  1023. }
  1024. }
  1025. usort($items, '_field_sort_items_value_helper');
  1026. // Add the items as table rows.
  1027. foreach ($items as $key => $item) {
  1028. $item['_weight']['#attributes']['class'] = array($order_class);
  1029. $delta_element = drupal_render($item['_weight']);
  1030. $cells = array(
  1031. array('data' => '', 'class' => array('field-multiple-drag')),
  1032. drupal_render($item),
  1033. array('data' => $delta_element, 'class' => array('delta-order')),
  1034. );
  1035. $rows[] = array(
  1036. 'data' => $cells,
  1037. 'class' => array('draggable', drupal_html_class('paragraphs_item_type_' . $item['#bundle'])),
  1038. );
  1039. }
  1040. $output = '<div class="form-item">';
  1041. if (count($items)) {
  1042. $output .= theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => $table_id, 'class' => array('field-multiple-table'))));
  1043. }
  1044. else {
  1045. $add_text = 'No !title_multiple added yet. Select a !title type and press the button below to add one.';
  1046. if ($add_mode == 'button') {
  1047. $add_text = 'No !title_multiple added yet. Select a !title type and press a button below to add one.';
  1048. }
  1049. $output .= '<label>' . t('!title !required', array('!title' => $element['#title'], '!required' => $required)) . "</label>";
  1050. $output .= '<p><em>' . t($add_text, array('!title_multiple' => t($instance['settings']['title_multiple']), '!title' => t($instance['settings']['title']))) . '</em></p>';
  1051. }
  1052. $output .= $element['#description'] ? '<div class="description">' . $element['#description'] . '</div>' : '';
  1053. $output .= '<div class="clearfix">' . drupal_render($add_more_button_type) . drupal_render($add_more_button) . '</div>';
  1054. $output .= '</div>';
  1055. drupal_add_tabledrag($table_id, 'order', 'sibling', $order_class);
  1056. return $output;
  1057. }
  1058. /**
  1059. * Implements hook_theme().
  1060. */
  1061. function paragraphs_theme() {
  1062. return array(
  1063. 'paragraphs_field_multiple_value_form' => array(
  1064. 'render element' => 'element',
  1065. ),
  1066. 'paragraphs_bundle_settings_form' => array(
  1067. 'render element' => 'form',
  1068. ),
  1069. 'paragraphs_items' => array(
  1070. 'render element' => 'element',
  1071. 'template' => 'paragraphs-items',
  1072. 'path' => drupal_get_path('module', 'paragraphs') . '/theme',
  1073. 'file' => 'paragraphs.theme.inc',
  1074. ),
  1075. 'paragraphs_item' => array(
  1076. 'render element' => 'elements',
  1077. 'template' => 'paragraphs-item',
  1078. 'path' => drupal_get_path('module', 'paragraphs') . '/theme',
  1079. 'file' => 'paragraphs.theme.inc',
  1080. ),
  1081. );
  1082. }
  1083. /**
  1084. * Implements hook_field_create_field().
  1085. */
  1086. function paragraphs_field_create_field($field) {
  1087. if ($field['type'] == 'paragraphs') {
  1088. // Clear caches.
  1089. entity_info_cache_clear();
  1090. // Do not directly issue menu rebuilds here to avoid potentially multiple
  1091. // rebuilds. Instead, let menu_get_item() issue the rebuild on the next
  1092. // request.
  1093. variable_set('menu_rebuild_needed', TRUE);
  1094. }
  1095. }
  1096. /**
  1097. * Implements hook_field_delete_field().
  1098. */
  1099. function paragraphs_field_delete_field($field) {
  1100. if ($field['type'] == 'paragraphs') {
  1101. // Clear caches.
  1102. entity_info_cache_clear();
  1103. // Do not directly issue menu rebuilds here to avoid potentially multiple
  1104. // rebuilds. Instead, let menu_get_item() issue the rebuild on the next
  1105. // request.
  1106. variable_set('menu_rebuild_needed', TRUE);
  1107. }
  1108. }
  1109. /**
  1110. * Implements hook_views_api().
  1111. */
  1112. function paragraphs_views_api() {
  1113. return array(
  1114. 'api' => '3.0-alpha1',
  1115. 'path' => drupal_get_path('module', 'paragraphs') . '/views',
  1116. );
  1117. }
  1118. /**
  1119. * Implements hook_module_implements_alter().
  1120. */
  1121. function paragraphs_module_implements_alter(&$implementations, $hook) {
  1122. switch ($hook) {
  1123. case 'field_attach_form':
  1124. // We put the implementation of field_attach_form implementation of
  1125. // paragraphs at the end, so it has a chance to disable the implementation
  1126. // of entity_translation that provides the form changes that will break
  1127. // paragraphs.
  1128. $group = $implementations['paragraphs'];
  1129. unset($implementations['paragraphs']);
  1130. $implementations['paragraphs'] = $group;
  1131. break;
  1132. }
  1133. }
  1134. /**
  1135. * Implements hook_field_attach_form().
  1136. */
  1137. function paragraphs_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) {
  1138. // We make sure paragraphs don't use the entity translation defaults, as those
  1139. // are not implemented properly yet in paragraphs. So we better show an empty
  1140. // initial field for a translation of an existing entity, than making
  1141. // paragraphs break completely.
  1142. // A proper implementation of entity_translation has still to be discussed.
  1143. // @see https://drupal.org/node/2152931
  1144. list( , , $bundle) = entity_extract_ids($entity_type, $entity);
  1145. foreach (field_info_instances($entity_type, $bundle) as $instance) {
  1146. $field_name = $instance['field_name'];
  1147. $field_info = field_info_field($field_name);
  1148. if ($field_info['type'] == 'paragraphs') {
  1149. if (isset($form[$field_name])) {
  1150. $element = &$form[$field_name];
  1151. // Remove the entity_translation preparion for the element. This way we
  1152. // avoid that there will be form elements that do not have a
  1153. // corresponding form state for the field.
  1154. if (!empty($element['#process'])) {
  1155. $key = array_search('entity_translation_prepare_element', $element['#process']);
  1156. if ($key !== FALSE) {
  1157. unset($element['#process'][$key]);
  1158. }
  1159. }
  1160. }
  1161. }
  1162. }
  1163. }
  1164. /**
  1165. * Implements hook_field_prepare_translation().
  1166. *
  1167. * @see field_attach_prepare_translation()
  1168. */
  1169. function paragraphs_field_prepare_translation($entity_type, $entity, $field, $instance, $langcode, &$items, $source_entity, $source_langcode) {
  1170. if (!module_exists("paragraphs_i18n")) {
  1171. list($id, , ) = entity_extract_ids($entity_type, $entity);
  1172. // field_attach_prepare_translation() copied the entity ids from the source,
  1173. // as we need a new entity for a new translation, we cannot reuse that.
  1174. // @todo clone existing paragraphs to new translation
  1175. if (empty($id)) {
  1176. $items = array();
  1177. }
  1178. } else {
  1179. paragraphs_i18n_field_prepare_translation($entity_type, $entity, $field, $instance, $langcode, $items, $source_entity, $source_langcode);
  1180. }
  1181. }
  1182. /**
  1183. * Implements hook_features_api().
  1184. */
  1185. function paragraphs_features_api() {
  1186. return array(
  1187. 'paragraphs' => array(
  1188. 'name' => t('Paragraphs Bundles'),
  1189. 'feature_source' => TRUE,
  1190. 'default_hook' => 'paragraphs_info',
  1191. 'file' => drupal_get_path('module', 'paragraphs') . '/paragraphs.features.inc',
  1192. ),
  1193. );
  1194. }
  1195. /**
  1196. * Implements hook_bundle_copy_info to provide a bundle copy
  1197. * export and import tab.
  1198. */
  1199. function paragraphs_bundle_copy_info() {
  1200. return array(
  1201. 'paragraphs_item' => array(
  1202. 'bundle_export_callback' => 'paragraphs_bundle_load',
  1203. 'bundle_save_callback' => 'paragraphs_bundle_save',
  1204. 'export_menu' => array(
  1205. 'path' => 'admin/structure/paragraphs/export',
  1206. 'access arguments' => 'administer content types',
  1207. ),
  1208. 'import_menu' => array(
  1209. 'path' => 'admin/structure/paragraphs/import',
  1210. 'access arguments' => 'administer content types',
  1211. ),
  1212. ),
  1213. );
  1214. }