/** * This is an example implementation for the hook. Preforms actions when converting a node based on it's type. * * @param $data * An array containing information about the conversion process. The keys are * - dest_node_type The destination type of the node * - node The node object * - Any other information passed by $op = 'options' or $op = 'options validate' * @param $op * A string containing the operation which should be executed. These are the possible values * - insert Operations which should be run when the node is transferred to the new node type. * Usually for transferring and adding new node information into the database. * - delete Operations which should be run after the node is transferred to the new node type. * Usually for deleting unneeded information from the database after the transfer. * - options Configuration elements shown on the conversion form. Should return a FAPI array. * - options validate Validation check on the options elements. * @return * Should return a FAPI array only when using the options operation. */ function hook_node_convert_change($data, $op) { // All of this is just an example. if ($op == 'insert') { if ($data['dest_node_type'] == 'book') { $book = array(); $node = $data['node']; $book['link_path'] = 'node/' . $node->nid; $book['link_title'] = $node->title; $book['plid'] = 0; $book['menu_name'] = book_menu_name($node->nid); $mlid = menu_link_save($book); $book['bid'] = $data['hook_options']['bid']; if ($book['bid'] == 'self') { $book['bid'] = $node->nid; } $id = db_insert('book')->fields(array('nid' => $node->nid, 'mlid' => $book['mlid'], 'bid' => $book['bid']))->execute(); } if ($data['dest_node_type'] == 'forum') { $id = db_insert('forum')->fields(array('tid' => $data['hook_options']['forum'], 'vid' => $data['node']->vid, 'nid' => $data['node']->nid))->execute(); $id = db_insert('taxonomy_term_node')->fields(array('tid' => $data['hook_options']['forum'], 'vid' => $data['node']->vid, 'nid' => $data['node']->nid))->execute(); } } elseif ($op == 'delete') { if ($data['node']->type == 'book') { menu_link_delete($data['node']->book['mlid']); db_delete('book')->condition('mlid', $data['node']->book['mlid'])->execute(); } if ($data['node']->type == 'forum') { db_delete('forum')->condition('nid', $data['node']->nid)->execute(); db_delete('taxonomy_term_node')->condition('nid', $data['node']->nid)->execute(); } } elseif ($op == 'options') { $form = array(); if ($data['dest_node_type'] == 'book') { foreach (book_get_books() as $book) { $options[$book['nid']] = $book['title']; } $options = array('self' => '<' . t('create a new book') . '>') + $options; $form['bid'] = array('#type' => 'select', '#title' => t('Book'), '#options' => $options, '#description' => t('Your page will be a part of the selected book.'), '#attributes' => array('class' => 'book-title-select')); } if ($data['dest_node_type'] == 'forum') { $vid = variable_get('forum_nav_vocabulary', ''); $form['forum'] = taxonomy_form($vid); $form['forum']['#weight'] = 7; $form['forum']['#required'] = TRUE; $form['forum']['#options'][''] = t('- Please choose -'); } return $form; } elseif ($op == 'options validate') { $form_state = $data['form_state']; if ($data['dest_node_type'] == 'forum') { $containers = variable_get('forum_containers', array()); $term = $form_state['values']['hook_options']['forum']; if (in_array($term, $containers)) { $term = taxonomy_term_load($term); form_set_error('hook_options][forum', t('The item %forum is only a container for forums. Please select one of the forums below it.', array('%forum' => $term->name))); } } } }
protected function createMenuItem($name, $node, $parent = null) { $item = ['link_path' => 'node/' . $node->nid, 'link_title' => $name, 'menu_name' => $this->menuName]; if ($parent) { if (empty($this->menuLinks[$parent])) { throw new \InvalidArgumentException("You are somehow stupid, and parent does not exist"); } $item['plid'] = $this->menuLinks[$parent]; } return $this->menuLinks[$name] = menu_link_save($item); }
/** * Create menu links. * * @param array $links * An array of menu link arrays. * @param array $defaults * An array of defaults to use for each link. This avoids having to repeat * values in each menu link, like 'menu_name' for example. */ public static function createLinks(array &$links, array $defaults = array()) { foreach ($links as &$link) { $link += $defaults; if (!url_is_external($link['link_path'])) { $link['link_path'] = drupal_get_normal_path($link['link_path']); } if ($mlid = menu_link_save($link)) { $link['mlid'] = $mlid; if (!empty($link['children'])) { static::createLinks($link['children'], array('plid' => $mlid) + $defaults); } } else { // Add error logging. } } }
function unl_reset_site_submit($form, &$form_state) { $nids = db_select('node', 'n')->fields('n', array('nid'))->execute()->fetchCol(); node_delete_multiple($nids); variable_set('site_frontpage', 'node'); $mlids = db_select('menu_links', 'm')->fields('m', array('mlid'))->condition('m.menu_name', 'main-menu')->execute()->fetchCol(); foreach ($mlids as $mlid) { menu_link_delete($mlid); } $home_link = array('link_path' => '<front>', 'link_title' => 'Home', 'menu_name' => 'main-menu', 'module' => 'menu'); menu_link_save($home_link); $fids = db_select('file_managed', 'f')->fields('f', array('fid'))->execute()->fetchCol(); $files = file_load_multiple($fids); foreach ($files as $file) { file_delete($file); } $files_dir = DRUPAL_ROOT . '/' . conf_path() . '/files/'; $cmd = 'rm -rf ' . $files_dir . '*'; echo shell_exec($cmd); drupal_mkdir('public://styles/'); variable_set('site_name', 'Site Name'); }
/** * Tests automatic reparenting. * * Runs tests on menu links defined by the menu_link.static service. */ function testMenuLinkRouterReparenting() { // Run all the standard parenting tests on menu links derived from // menu routers. $this->testMenuLinkReparenting('system'); // Additionnaly, test reparenting based on path. $links = $this->createLinkHierarchy('system'); // Move child-1-2 has a child of child-2, making the link hierarchy // inconsistent with the path hierarchy. $links['child-1-2']['plid'] = $links['child-2']['mlid']; menu_link_save($links['child-1-2']); // Check the new hierarchy. $expected_hierarchy = array('parent' => FALSE, 'child-1' => 'parent', 'child-1-1' => 'child-1', 'child-2' => 'parent', 'child-1-2' => 'child-2'); $this->assertMenuLinkParents($links, $expected_hierarchy); // Now delete 'parent' directly from the database, simulating a database // crash. 'child-1' and 'child-2' should get moved to the // top-level. // Don't do that at home. db_delete('menu_links')->condition('mlid', $links['parent']['mlid'])->execute(); $expected_hierarchy = array('child-1' => FALSE, 'child-1-1' => 'child-1', 'child-2' => FALSE, 'child-1-2' => 'child-2'); $this->assertMenuLinkParents($links, $expected_hierarchy); // Now delete 'child-2' directly from the database, simulating a database // crash. 'child-1-2' will get reparented to the top. // Don't do that at home. db_delete('menu_links')->condition('mlid', $links['child-2']['mlid'])->execute(); $expected_hierarchy = array('child-1' => FALSE, 'child-1-1' => 'child-1', 'child-1-2' => FALSE); $this->assertMenuLinkParents($links, $expected_hierarchy); }
/** * Test creating, editing, deleting menu links via node form widget. */ function testMenuNodeFormWidget() { // Disable the default main menu, so that no menus are enabled. $edit = array('menu_options[main]' => FALSE); $this->drupalPostForm('admin/structure/types/manage/page', $edit, t('Save content type')); // Verify that no menu settings are displayed and nodes can be created. $this->drupalGet('node/add/page'); $this->assertText(t('Create Basic page')); $this->assertNoText(t('Menu settings')); $node_title = $this->randomName(); $edit = array('title[0][value]' => $node_title, 'body[0][value]' => $this->randomString()); $this->drupalPostForm(NULL, $edit, t('Save')); $node = $this->drupalGetNodeByTitle($node_title); $this->assertEqual($node->getTitle(), $edit['title[0][value]']); // Enable Tools menu as available menu. $edit = array('menu_options[main]' => 1, 'menu_options[tools]' => 1, 'menu_parent' => 'main:0'); $this->drupalPostForm('admin/structure/types/manage/page', $edit, t('Save content type')); // Create a node. $node_title = $this->randomName(); $edit = array('title[0][value]' => $node_title, 'body[0][value]' => $this->randomString()); $this->drupalPostForm('node/add/page', $edit, t('Save')); $node = $this->drupalGetNodeByTitle($node_title); // Assert that there is no link for the node. $this->drupalGet('test-page'); $this->assertNoLink($node_title); // Edit the node, enable the menu link setting, but skip the link title. $edit = array('menu[enabled]' => 1); $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save')); // Assert that there is no link for the node. $this->drupalGet('test-page'); $this->assertNoLink($node_title); // Edit the node and create a menu link. $edit = array('menu[enabled]' => 1, 'menu[link_title]' => $node_title, 'menu[weight]' => 17); $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save')); // Assert that the link exists. $this->drupalGet('test-page'); $this->assertLink($node_title); $this->drupalGet('node/' . $node->id() . '/edit'); $this->assertOptionSelected('edit-menu-weight', 17, 'Menu weight correct in edit form'); // Edit the node and remove the menu link. $edit = array('menu[enabled]' => FALSE); $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save')); // Assert that there is no link for the node. $this->drupalGet('test-page'); $this->assertNoLink($node_title); // Add a menu link to the Administration menu. $item = entity_create('menu_link', array('link_path' => 'node/' . $node->id(), 'link_title' => $this->randomName(16), 'menu_name' => 'admin')); $item->save(); // Assert that disabled Administration menu is not shown on the // node/$nid/edit page. $this->drupalGet('node/' . $node->id() . '/edit'); $this->assertText('Provide a menu link', 'Link in not allowed menu not shown in node edit form'); // Assert that the link is still in the Administration menu after save. $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save')); $link = menu_link_load($item['mlid']); $this->assertTrue($link, 'Link in not allowed menu still exists after saving node'); // Move the menu link back to the Tools menu. $item['menu_name'] = 'tools'; menu_link_save($item); // Create a second node. $child_node = $this->drupalCreateNode(array('type' => 'article')); // Assign a menu link to the second node, being a child of the first one. $child_item = entity_create('menu_link', array('link_path' => 'node/' . $child_node->id(), 'link_title' => $this->randomName(16), 'plid' => $item['mlid'])); $child_item->save(); // Edit the first node. $this->drupalGet('node/' . $node->id() . '/edit'); // Assert that it is not possible to set the parent of the first node to itself or the second node. $this->assertNoOption('edit-menu-parent', 'tools:' . $item['mlid']); $this->assertNoOption('edit-menu-parent', 'tools:' . $child_item['mlid']); // Assert that unallowed Administration menu is not available in options. $this->assertNoOption('edit-menu-parent', 'admin:0'); }
/** * {@inheritdoc} */ public function moveBefore($itemId, $otherItemId) { list(, , $parentId, $weight) = $this->validateMove($itemId, $otherItemId); $this->db->query("UPDATE {menu_links} SET weight = weight - 2 WHERE plid = :plid AND mlid <> :mlid AND weight <= :neww", [':mlid' => $otherItemId, ':plid' => $parentId, ':neww' => $weight]); $item = menu_link_load($itemId); $item['weight'] = $weight - 1; $item['plid'] = $parentId; if (!menu_link_save($item)) { throw new \RuntimeException("Could not save item"); } }
/** * Test menu link 'options' storage and rendering. */ protected function doTestMenuLinkOptions() { // Create a menu link with options. $menu_link = entity_create('menu_link', array('link_title' => 'Menu link options test', 'link_path' => 'test-page', 'module' => 'menu_test', 'options' => array('attributes' => array('title' => 'Test title attribute'), 'query' => array('testparam' => 'testvalue')))); menu_link_save($menu_link); // Load front page. $this->drupalGet('test-page'); $this->assertRaw('title="Test title attribute"', 'Title attribute of a menu link renders.'); $this->assertRaw('testparam=testvalue', 'Query parameter added to menu link.'); }
private function eventSpeichern() { if (isset($_FILES['bild']['name']) && !empty($_FILES['bild']['name'])) { // TODO: try-catch with $this->imageUploadable() $this->bild = $this->upload_image($_FILES['bild']); } if ($this->isFestival) { $this->FID = str_replace('f', '', $this->veranstalter); } $BID = $this->adresse->bezirk; $this->adresse = $this->adressHelper->setUpdateAdresse($this->adresse); $startQuery = $this->start . ' ' . (!empty($this->zeit_von) ? $this->zeit_von . ':01' : '00:00:00'); $endeQuery = (!empty($this->ende) ? $this->ende : '1000-01-01') . ' ' . (!empty($this->zeit_bis) ? $this->zeit_bis . ':01' : '00:00:00'); $this->event_id = db_insert($this->tbl_event)->fields(array('name' => $this->name, 'ort' => $this->adresse, 'start_ts' => $startQuery, 'url' => $this->url, 'ende_ts' => $endeQuery, 'bild' => $this->bild, 'kurzbeschreibung' => $this->kurzbeschreibung, 'ersteller' => $this->user_id, 'created' => date('Y-m-d H:i:s', time()), 'recurring_event_type' => $this->eventRecurres && !empty($this->eventRecurringType) ? $this->eventRecurringType : NULL, 'event_recurres_till' => $this->eventRecurres && !empty($this->eventRecurresTill) ? $this->eventRecurresTill . ' 00:00:00' : '1000-01-01 00:00:00', 'FID' => $this->FID))->execute(); if (!empty($this->isFestival)) { db_update($this->tbl_event)->fields(array('recurring_event_type' => '6'))->condition('EID', $this->event_id)->execute(); $this->veranstalter = db_query($this->tbl_akteur_festivals, 'f')->fields('f', array('admin'))->condition('FID', $this->FID)->execute()->fetchObject(); $this->veranstalter = $this->veranstalter->admin; } // if Veranstalter != 'privat' if (!empty($this->veranstalter)) { db_insert($this->tbl_akteur_events)->fields(array('AID' => $this->veranstalter, 'EID' => $this->event_id))->execute(); } $this->tagsHelper->setRemoveTags($this->tags, array('event', $this->event_id), $this->removedTags); if ($this->eventRecurres == 'on' && !empty($this->eventRecurringType)) { $recurresTill = !empty($this->eventRecurresTill) ? (new \DateTime($this->eventRecurresTill))->format('Y-m-d') : NULL; $this->event->addEventChildren($this->event_id, $this->eventRecurringType, $startQuery, $endeQuery, $recurresTill); } // Tell Drupal about the new eventprofil/ID-item, get Bezirksname $parentItem = db_query("SELECT menu_links.mlid\n FROM {menu_links} menu_links\n WHERE menu_name = :menu_name AND link_path = :link_path", array(":menu_name" => "navigation", ":link_path" => 'events')); $bezirk = db_select($this->tbl_bezirke, 'b')->fields('b')->condition('BID', $BID)->execute(); $plid = $parentItem->fetchObject(); $bezirk = trim(preg_replace("/\\(\\w+\\)/", "", $bezirk->fetchObject()->bezirksname)); $item = array('menu_name' => 'navigation', 'weight' => 1, 'link_title' => t('!name am !datum in !ort | Events', array('!name' => $this->name, '!datum' => (new \DateTime($startQuery))->format('d.m.Y'), '!ort' => $bezirk)), 'module' => 'aae_data', 'link_path' => 'eventprofil/' . $this->event_id, 'plid' => $plid->mlid); menu_link_save($item); # SEO: ['options']['attributes']['description'] ? // Call hooks module_invoke_all('hook_event_created'); if (session_status() == PHP_SESSION_NONE) { session_start(); } drupal_set_message(t('Das Event wurde erfolgreich erstellt!')); if ($this->isFestival) { header('Location: ' . $base_url . '/events/new'); } else { header('Location: ' . $base_url . '/eventprofil/' . $this->event_id); } }
/** * {@inheritdoc} */ public function synchronize(NodeInterface $node, Context $context, $dirtyAllowed = false) { /* @var $node \USync\AST\Drupal\MenuItemNode */ $object = ['menu_name' => $node->getMenuName(), 'customized' => 1, 'weight' => $node->getPosition()]; if ($node->hasChild('name')) { $object['link_title'] = (string) $node->getChild('name')->getValue(); } if ($node->hasChild('path')) { $object['link_path'] = (string) $node->getChild('path')->getValue(); } if ($node->hasChild('expanded')) { $object['expanded'] = (int) (bool) $node->getChild('expanded')->getValue(); } if ($node->hasChild('hidden')) { $object['hidden'] = (int) (bool) $node->getChild('hidden')->getValue(); } if ($node->hasChild('options')) { $object['options'] = (array) $node->getChild('options')->getValue(); if (!empty($object['options'])) { // @todo Should merge with existing, maybe, or defaults ? } } $object += self::$defaults; if ($mlid = $this->findMenuItemId($node, $context)) { $object['mlid'] = $mlid; } // Find parent - no matter how hard it is. // First one is "menu", second one is the real parent. $parent = $node->getParentMenuItem(); if ($parent) { if ($plid = $parent->getDrupalIdentifier()) { $object['plid'] = $plid; } } if (empty($object['plid'])) { $object['plid'] = 0; } // Phoque zate. $object['hidden'] = (int) (bool) $object['hidden']; $object['expanded'] = (int) (bool) $object['expanded']; $id = menu_link_save($object); // It seems that, sometime, this doesn't get called... _menu_update_parental_status($object); return $id; }
/** * Tests menu functionality using the admin and user interfaces. */ function testMenu() { // Login the user. $this->drupalLogin($this->admin_user); $this->items = array(); $this->menu = $this->addCustomMenu(); $this->doMenuTests(); $this->addInvalidMenuLink(); $this->addCustomMenuCRUD(); // Verify that the menu links rebuild is idempotent and leaves the same // number of links in the table. $before_count = db_query('SELECT COUNT(*) FROM {menu_links}')->fetchField(); menu_link_rebuild_defaults(); $after_count = db_query('SELECT COUNT(*) FROM {menu_links}')->fetchField(); $this->assertIdentical($before_count, $after_count, 'menu_link_rebuild_defaults() does not add more links'); // Do standard user tests. // Login the user. $this->drupalLogin($this->authenticated_user); $this->verifyAccess(403); foreach ($this->items as $item) { // Paths were set as 'node/$nid'. $node = node_load(substr($item['link_path'], 5)); $this->verifyMenuLink($item, $node); } // Login the administrator. $this->drupalLogin($this->admin_user); // Delete menu links. foreach ($this->items as $item) { $this->deleteMenuLink($item); } // Delete custom menu. $this->deleteCustomMenu(); // Modify and reset a standard menu link. $item = $this->getStandardMenuLink(); $old_title = $item['link_title']; $this->modifyMenuLink($item); $item = entity_load('menu_link', $item['mlid']); // Verify that a change to the description is saved. $description = $this->randomName(16); $item['options']['attributes']['title'] = $description; $return_value = menu_link_save($item); // Save the menu link again to test the return value of the procedural save // helper. $this->assertIdentical($return_value, $item->save(), 'Return value of menu_link_save() is identical to the return value of $menu_link->save().'); $saved_item = entity_load('menu_link', $item['mlid']); $this->assertEqual($description, $saved_item['options']['attributes']['title'], 'Saving an existing link updates the description (title attribute)'); $this->resetMenuLink($item, $old_title); }
/** * Overrides Drupal\configuration\Config\Configuration::saveToActiveStore(). */ public function saveToActiveStore(ConfigIteratorSettings &$settings) { $data = $this->getData(); // Determine if the menu already exists. $data['mlid'] = static::getMenuLinkByIdenfifier($this->getIdentifier()); if (!empty($data['parent_identifier'])) { $data['plid'] = static::getMenuLinkByIdenfifier($this->data['parent_identifier'], TRUE); } menu_link_save($data); $settings->addInfo('imported', $this->getUniqueId()); }
/** * Schreibt Daten in DB */ private function festivalSpeichern() { $gps = explode(',', $this->gps, 2); $this->adresse = db_insert($this->tbl_adresse)->fields(array('strasse' => $this->strasse, 'nr' => $this->nr, 'adresszusatz' => $this->adresszusatz, 'plz' => $this->plz, 'bezirk' => $this->ort, 'gps_lat' => $gps[0], 'gps_long' => $gps[1]))->execute(); //Wenn Bilddatei ausgewählt wurde... if (isset($_FILES['bild']['name']) && !empty($_FILES['bild']['name'])) { $this->bild = $this->upload_image($_FILES['bild']); } $this->akteur_id = db_insert($this->tbl_akteur)->fields(array('name' => $this->name, 'adresse' => $this->adresse, 'email' => $this->email, 'telefon' => $this->telefon, 'url' => $this->url, 'ansprechpartner' => $this->ansprechpartner, 'funktion' => $this->funktion, 'bild' => $this->bild, 'beschreibung' => $this->beschreibung, 'oeffnungszeiten' => $this->oeffnungszeiten, 'barrierefrei' => isset($_POST['barrierefrei']) && !empty($_POST['barrierefrei']) ? '1' : '0', 'ersteller' => $this->user_id, 'created' => date('Y-m-d H:i:s', time())))->execute(); db_insert($this->tbl_hat_user)->fields(array('hat_UID' => $this->user_id, 'hat_AID' => $this->akteur_id))->execute(); // Tell Drupal about new festival-item $parentItem = db_query("SELECT menu_links.mlid\n FROM {menu_links} menu_links\n WHERE menu_name = :menu_name AND link_path = :link_path", array(":menu_name" => "navigation", ":link_path" => 'festivals')); $plid = $parentItem->fetchObject(); $item = array('menu_name' => 'navigation', 'weight' => 1, 'link_title' => t('Festivalseite von !username', array('!username' => $this->name)), 'module' => 'aae_data', 'link_path' => 'akteurprofil/' . $this->akteur_id, 'plid' => $plid->mlid); menu_link_save($item); if (session_status() == PHP_SESSION_NONE) { session_start(); } drupal_set_message(t('Das Festival wurde erfolgreich erstellt!')); header('Location: ' . $base_url . '/' . $this->alias); }
<?php // Just do stuff :) //$n = node_load(1); //print $n->title . '\n'; // Let's get rid of the unsightly user login block db_delete('block')->condition('module', 'user')->execute(); // Get difficult login link into user menu $link = array('link_title' => 'Log in', 'link_path' => 'user/login', 'menu_name' => 'user-menu', 'weight' => 0, 'expanded' => 0); $item = array('link_path' => $link['link_path'], 'link_title' => $link['link_title'], 'menu_name' => $link['menu_name'], 'weight' => $link['weight'], 'expanded' => $link['expanded']); $exists = db_query("SELECT mlid from {menu_links} WHERE link_title=:link_title AND link_path=:link_path", array(':link_title' => $link['link_title'], ':link_path' => $link['link_path']))->fetchField(); // Save the record if the data does not exist if (!$exists) { menu_link_save($item); menu_rebuild(); }