menu_links.inc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. /**
  3. * @file
  4. * Support for menu link destinations.
  5. */
  6. /**
  7. * Destination class implementing migration into {menu_links}.
  8. */
  9. class MigrateDestinationMenuLinks extends MigrateDestination {
  10. static public function getKeySchema() {
  11. return array(
  12. 'mlid' => array(
  13. 'type' => 'int',
  14. 'unsigned' => TRUE,
  15. 'not null' => TRUE,
  16. 'description' => 'ID of destination link',
  17. ),
  18. );
  19. }
  20. public function __construct() {
  21. parent::__construct();
  22. }
  23. public function __toString() {
  24. $output = t('Menu links');
  25. return $output;
  26. }
  27. /**
  28. * Returns a list of fields available to be mapped for menu links.
  29. *
  30. * @param Migration $migration
  31. * Optionally, the migration containing this destination.
  32. * @return array
  33. * Keys: machine names of the fields (to be passed to addFieldMapping)
  34. * Values: Human-friendly descriptions of the fields.
  35. */
  36. public function fields($migration = NULL) {
  37. $fields = array(
  38. 'menu_name' => t('The menu name. All links with the same menu name (such as \'navigation\') are part of the same menu.'),
  39. 'mlid' => t('The menu link ID (mlid) is the integer primary key.'),
  40. 'plid' => t('The parent link ID (plid) is the mlid of the link above in the hierarchy, or zero if the link is at the top level in its menu.'),
  41. 'link_path' => t('The Drupal path or external path this link points to.'),
  42. 'router_path' => t('For links corresponding to a Drupal path (external = 0), this connects the link to a {menu_router}.path for joins.'),
  43. 'link_title' => t('The text displayed for the link, which may be modified by a title callback stored in {menu_router}.'),
  44. 'options' => t('A serialized array of options to be passed to the url() or l() function, such as a query string or HTML attributes.'),
  45. 'module' => t('The name of the module that generated this link.'),
  46. 'hidden' => t('A flag for whether the link should be rendered in menus. (1 = a disabled menu item that may be shown on admin screens, -1 = a menu callback, 0 = a normal, visible link)'),
  47. 'external' => t('A flag to indicate if the link points to a full URL starting with a protocol, like http:// (1 = external, 0 = internal).'),
  48. 'has_children' => t('Flag indicating whether any links have this link as a parent (1 = children exist, 0 = no children).'),
  49. 'expanded' => t('Flag for whether this link should be rendered as expanded in menus - expanded links always have their child links displayed, instead of only when the link is in the active trail (1 = expanded, 0 = not expanded)'),
  50. 'weight' => t('Link weight among links in the same menu at the same depth.'),
  51. 'depth' => t('The depth relative to the top level. A link with plid == 0 will have depth == 1.'),
  52. 'customized' => t('A flag to indicate that the user has manually created or edited the link (1 = customized, 0 = not customized).'),
  53. 'p1' => t('The first mlid in the materialized path. If N = depth, then pN must equal the mlid. If depth > 1 then p(N-1) must equal the plid. All pX where X > depth must equal zero. The columns p1 .. p9 are also called the parents.'),
  54. 'p2' => t('The second mlid in the materialized path. See p1.'),
  55. 'p3' => t('The third mlid in the materialized path. See p1.'),
  56. 'p4' => t('The fourth mlid in the materialized path. See p1.'),
  57. 'p5' => t('The fifth mlid in the materialized path. See p1.'),
  58. 'p6' => t('The sixth mlid in the materialized path. See p1.'),
  59. 'p7' => t('The seventh mlid in the materialized path. See p1.'),
  60. 'p8' => t('The eighth mlid in the materialized path. See p1.'),
  61. 'p9' => t('The ninth mlid in the materialized path. See p1.'),
  62. 'updated' => t('Flag that indicates that this link was generated during the update from Drupal 5.'),
  63. );
  64. return $fields;
  65. }
  66. /**
  67. * Import a single row.
  68. *
  69. * @param $menu_link
  70. * Menu link object to build. Prefilled with any fields mapped in the Migration.
  71. * @param $row
  72. * Raw source data object - passed through to prepare/complete handlers.
  73. * @return array
  74. * Array of key fields of the object that was saved if
  75. * successful. FALSE on failure.
  76. */
  77. public function import(stdClass $menu_link, stdClass $row) {
  78. // Updating previously-migrated content
  79. if (isset($row->migrate_map_destid1)) {
  80. $menu_link->mlid = $row->migrate_map_destid1;
  81. }
  82. // Invoke migration prepare handlers
  83. // @todo derive existing mlids?
  84. $this->prepare($menu_link, $row);
  85. // Menu links are handled as arrays, so clone the object to an array.
  86. $item = clone $menu_link;
  87. $item = (array) $item;
  88. migrate_instrument_start('menu_link_save');
  89. // Check to see if this is a new menu item.
  90. $update = FALSE;
  91. if (isset($item['mlid'])) {
  92. $update = TRUE;
  93. $mlid = menu_link_save($item);
  94. }
  95. else {
  96. // menu_link_save() should return an mlid integer.
  97. $mlid = menu_link_save($item);
  98. }
  99. migrate_instrument_stop('menu_link_save');
  100. // Return the new id or FALSE on failure.
  101. if (!empty($mlid)) {
  102. // Increment the count if the save succeeded.
  103. if ($update) {
  104. $this->numUpdated++;
  105. }
  106. else {
  107. $this->numCreated++;
  108. }
  109. // Return the primary key to the mapping table.
  110. $return = array($mlid);
  111. }
  112. else {
  113. $return = FALSE;
  114. }
  115. // Invoke migration complete handlers.
  116. $menu_link = (object) menu_link_load($mlid);
  117. $this->complete($menu_link, $row);
  118. return $return;
  119. }
  120. /**
  121. * Implementation of MigrateDestination::prepare().
  122. */
  123. public function prepare($menu_link, stdClass $row) {
  124. // We do nothing here but allow child classes to act.
  125. $migration = Migration::currentMigration();
  126. $menu_link->migrate = array(
  127. 'machineName' => $migration->getMachineName(),
  128. );
  129. // Call any general handlers.
  130. migrate_handler_invoke_all('menu_links', 'prepare', $menu_link, $row);
  131. // Then call any prepare handler for this specific Migration.
  132. if (method_exists($migration, 'prepare')) {
  133. $migration->prepare($menu_link, $row);
  134. }
  135. }
  136. /**
  137. * Implementation of MigrateDestination::complete().
  138. */
  139. public function complete($menu_link, stdClass $row) {
  140. // We do nothing here but allow child classes to act.
  141. $migration = Migration::currentMigration();
  142. $menu_link->migrate = array(
  143. 'machineName' => $migration->getMachineName(),
  144. );
  145. // Call any general handlers.
  146. migrate_handler_invoke_all('menu_links', 'complete', $menu_link, $row);
  147. // Then call any complete handler for this specific Migration.
  148. if (method_exists($migration, 'complete')) {
  149. $migration->complete($menu_link, $row);
  150. }
  151. }
  152. /**
  153. * Implementation of MigrateDestination::postImport().
  154. */
  155. public function postImport() {
  156. // Clear the cache after all menu links are imported.
  157. menu_cache_clear_all();
  158. }
  159. /**
  160. * Delete a single menu item.
  161. *
  162. * @param $id
  163. * Array of fields representing the key (in this case, just mlid).
  164. */
  165. public function rollback($id) {
  166. $mlid = reset($id);
  167. migrate_instrument_start('menu_link_delete');
  168. $this->prepareRollback($mlid);
  169. // @todo: any error checking here? For example, menu.inc has:
  170. // if ($menu = menu_load($menu_name)) { menu_delete($menu) }
  171. menu_link_delete($mlid);
  172. $this->completeRollback($mlid);
  173. migrate_instrument_stop('menu_link_delete');
  174. }
  175. /**
  176. * Give handlers a shot at cleaning up before a menu has been rolled back.
  177. *
  178. * @param $mlid
  179. * ID of the menu link about to be deleted.
  180. */
  181. public function prepareRollback($mlid) {
  182. // We do nothing here but allow child classes to act.
  183. $migration = Migration::currentMigration();
  184. // Call any general handlers.
  185. migrate_handler_invoke_all('menu_links', 'prepareRollback', $mlid);
  186. // Then call any complete handler for this specific Migration.
  187. if (method_exists($migration, 'prepareRollback')) {
  188. $migration->prepareRollback($mlid);
  189. }
  190. }
  191. /**
  192. * Give handlers a shot at cleaning up after a menu has been rolled back.
  193. *
  194. * @param $mlid
  195. * ID of the menu link which has been deleted.
  196. */
  197. public function completeRollback($mlid) {
  198. // We do nothing here but allow child classes to act.
  199. $migration = Migration::currentMigration();
  200. // Call any general handlers.
  201. migrate_handler_invoke_all('menu_links', 'completeRollback', $mlid);
  202. // Then call any complete handler for this specific Migration.
  203. if (method_exists($migration, 'completeRollback')) {
  204. $migration->completeRollback($mlid);
  205. }
  206. }
  207. /**
  208. * Implementation of MigrateDestination::postRollback().
  209. */
  210. public function postRollback() {
  211. // Clear the cache after all menu links are rolled back.
  212. menu_cache_clear_all();
  213. }
  214. }