Commit 9f1b0a4d authored by Reena Raghavan's avatar Reena Raghavan

Modified

parent 49ac4014
......@@ -6,6 +6,13 @@
use Drupal\node\Entity\Node;
use Drupal\media\Entity\Media;
use Drupal\file\Entity\File;
use Drupal\Core\Controller\ControllerBase;
use Drupal\layout_builder\Section;
use Drupal\layout_builder\SectionComponent;
use Drupal\block\Entity\Block;
use Drupal\block_content\Entity\BlockContent;
use Drupal\layout_builder\Entity;
use PhpParser\Node\Expr\Print_;
/**
* Service description.
......@@ -36,6 +43,20 @@ public function getData()
*/
public function importData()
{
// Delete all nodes of given content type.
$storage_handler = \Drupal::entityTypeManager()->getStorage('node');
$nodes = $storage_handler->loadByProperties([
'type' => 'stories',
]);
$storage_handler->delete($nodes);
// Delete content type.
$content_type = \Drupal::entityTypeManager()
->getStorage('node_type')
->load('stories');
$content_type->delete();
exit();
/**
* Import data from a CSV file.
*
......@@ -50,50 +71,114 @@ public function importData()
//$line is an array of the csv elements
if ($line[0] != 'nid') {
$nid = $line[0];
$node_storage = \Drupal::entityTypeManager()->getStorage(
'node'
);
$node = $node_storage->load($nid);
//echo $node->title->value; // "Lorem Ipsum..."
echo $nid = $line[0];
$values = \Drupal::entityQuery('node')
->condition('nid', $nid)
->execute();
$node_exists = !empty($values);
// Author Simple Start
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
$author_simple = $line[17];
preg_match_all(
'#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#',
$author_simple,
$match
);
// Author Simple
$social_media_url = $match[0];
$strings = explode('https://', $author_simple);
$name_surname = $strings[0];
echo '<pre>';
echo $name_surname;
echo '</pre>';
echo '<pre>';
echo $social_media_url[0];
echo '</pre>';
$file_id = $line[16];
$file = \Drupal\file\Entity\File::load($file_id);
$uri = $file->getFileUri();
$url = file_create_url($uri);
echo '<pre>';
echo $url;
echo '</pre>';
$media = Media::load($line[16]);
echo '<pre>';
print_r($media);
echo '</pre>';
echo '------------------'; // new line
$name_surname; // Name & Surname
$social_media_url[0]; // Social Media URL
if ($node_exists == true) {
$node_storage = \Drupal::entityTypeManager()->getStorage(
'node'
);
$node = $node_storage->load($nid);
$entity = Node::load($nid);
$block_content = BlockContent::create([
'type' => 'message',
'info' => 'Demo Block1',
'reusable' => '0',
'body' => [
[
'value' => 'This is the block content1',
'format' => filter_default_format(),
],
'reusable' => '0',
],
]);
$block_content->set('body', $name_surname);
$block_content->save();
$block_ids['first'] = $block_content->getRevisionId();
$block_content = BlockContent::create([
'type' => 'message',
'info' => 'Demo Block2',
'reusable' => '0',
'body' => [
[
'value' => 'This is the block content2',
'format' => filter_default_format(),
],
'reusable' => '0',
],
]);
$block_content->set('body', $social_media_url[0]);
$block_content->save();
$block_ids['second'] = $block_content->getRevisionId();
$section = new Section('layout_twocol_section');
\Drupal::logger('uvf_migrate')->notice('called once');
foreach ($block_ids as $region => $block_revision_id) {
$pluginConfiguration = [
'id' => 'inline_block:message',
'label' => 'Layout Builder Inline Block',
'provider' => 'layout_builder',
'label_display' => '0',
'view_mode' => 'full',
'block_revision_id' => $block_revision_id,
'block_serialized' => null,
'context_mapping' => [],
];
$component = new SectionComponent(
\Drupal::service('uuid')->generate(),
'first',
$pluginConfiguration
);
$section->appendComponent($component);
}
$entity->layout_builder__layout->setValue($section);
$entity->save();
}
// Author Simple End
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Media Update Start
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// $file_id = $line[16];
// $media = Media::load($file_id);
// $media->field_media_image->target_id = $file_id;
// $media->save();
// Media Update End
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Thumbnail Update Start
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// if ($node_exists == true) {
// $node_storage = \Drupal::entityTypeManager()->getStorage(
// 'node'
// );
// $node = $node_storage->load($nid);
// $node->set('field_thumbnail', ['target_id' => $file_id]);
// $node->save();
// }
// Thumbnail Update End
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo '<pre>';
print_r($line);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment