If you need to import pages from another system to Drupal 8 you can get inspired by following code.
Site structure is defined in XML file. Following example will create two pages with menu links and one independent menu link pointing to /atlas URL.
XML file is processed in ImportForm controller.
Site structure is defined in XML file. Following example will create two pages with menu links and one independent menu link pointing to /atlas URL.
page Programming programming.html true Programming Programming -45 /programming page Drupal 8 - Basic HTML Block drupal8-html-block.html true Drupal 8 Basic HTML Block Drupal 8 Basic HTML Block /programming -50 /drupal8-html-block link Atlas true Atlas Atlas -48 /atlas
XML file is processed in ImportForm controller.
namespace Drupal\page_importer\Form; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\node\NodeInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\Url; use Drupal\menu_link_content\Entity\MenuLinkContent; use Drupal\microatlas\Service\AtlasTranslator; use Symfony\Component\HttpFoundation\RedirectResponse; use Drupal\microatlas\Service\Drupal\microatlas\Service; use Drupal\microatlas\Model\Category; use Drupal\microatlas\Service\AtlasRepository; use Drupal\Core\File\FileSystem; use Drupal\microatlas\Model\Entry; use Drupal\microatlas\Model\Title; use Drupal\microatlas\Model\Content; use Drupal\microatlas\Model\Link; class ImportForm extends FormBase { function getFormID() { return 'page_importer'; } public function buildForm(array $form, FormStateInterface $form_state) { $user = \Drupal::currentUser(); if ($user == null || $user->id () == 0) { return; } $form ['path'] = array ( '#type' => 'textfield', '#title' => $this->t('Path to xml'), '#default_value' => realpath(\Drupal::service('extension.list.module')->getPath('page_importer').'/data'), ); $form ['actions'] ['submit'] = array ( '#type' => 'submit', '#value' => $this->t ( 'Submit' ) ); return $form; } public function validateForm(array &$form, FormStateInterface $form_state) { } public function submitForm(array &$form, FormStateInterface $form_state) { $categories_count = 0; $entries_count = 0; $path = $form_state->getValue("path"); $pages = simplexml_load_file($path.'/pages.xml'); $count = 0; $menuItems = array(); foreach ($pages->page as $page) { $parentId = ""; if ($page->menu_parent_item != "") { $parentId = $menuItems[(string)$page->menu_parent_item]->getPluginId(); } if ((string)$page->type == 'link') { $menuItem = MenuLinkContent::create([ 'title' => (string)$page->title, 'link' => ['uri' => 'internal:'.(string)$page->url_alias], 'menu_name' => 'main', 'expanded' => 1, 'parent' => $parentId, ]); $menuItem->save(); $menuItems[(string)$page->url_alias] = $menuItem; } else { $new_page_values = array(); $new_page_values['type'] = (string)$page->type; $new_page_values['title'] = $page->title; $new_page_values['path'] = $page->url_alias; $body = file_get_contents($path.'/pages/'.$page->body_file); $new_page_values['body'] = array( 'value' => $body, 'format' => 'full_html', ); $node = \Drupal::entityTypeManager()->getStorage('node')->create($new_page_values); //$node = entity_create('node', $new_page_values); $node->save(); if ($page->provide_menu_link == 'true') { $menuItem = MenuLinkContent::create([ 'title' => (string)$page->title, 'link' => ['uri' => 'internal:'.(string)$page->url_alias], 'menu_name' => 'main', 'expanded' => 1, 'parent' => $parentId, ]); $menuItem->save(); $menuItems[(string)$page->url_alias] = $menuItem; } } $count = $count +1; } \Drupal::messenger()->addMessage(t('Number of created pages :@count.', array('@count' => $count))); } }
If you need some help with importing pages to Drupal 8 or Alfresco CMS, you can contact me via email katalpa.cz@gmail.com.