Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
U
UVF
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Packages
Packages
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Reena Raghavan
UVF
Commits
9f1b0a4d
Commit
9f1b0a4d
authored
Dec 09, 2022
by
Reena Raghavan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Modified
parent
49ac4014
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
120 additions
and
35 deletions
+120
-35
web/modules/custom/uvf_migrate/src/Importcsv.php
web/modules/custom/uvf_migrate/src/Importcsv.php
+120
-35
No files found.
web/modules/custom/uvf_migrate/src/Importcsv.php
View file @
9f1b0a4d
...
@@ -6,6 +6,13 @@
...
@@ -6,6 +6,13 @@
use
Drupal\node\Entity\Node
;
use
Drupal\node\Entity\Node
;
use
Drupal\media\Entity\Media
;
use
Drupal\media\Entity\Media
;
use
Drupal\file\Entity\File
;
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.
* Service description.
...
@@ -36,6 +43,20 @@ public function getData()
...
@@ -36,6 +43,20 @@ public function getData()
*/
*/
public
function
importData
()
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.
* Import data from a CSV file.
*
*
...
@@ -50,50 +71,114 @@ public function importData()
...
@@ -50,50 +71,114 @@ public function importData()
//$line is an array of the csv elements
//$line is an array of the csv elements
if
(
$line
[
0
]
!=
'nid'
)
{
if
(
$line
[
0
]
!=
'nid'
)
{
$nid
=
$line
[
0
];
echo
$nid
=
$line
[
0
];
$node_storage
=
\Drupal
::
entityTypeManager
()
->
getStorage
(
$values
=
\Drupal
::
entityQuery
(
'node'
)
'node'
->
condition
(
'nid'
,
$nid
)
);
->
execute
();
$node
=
$node_storage
->
load
(
$nid
);
$node_exists
=
!
empty
(
$values
);
//echo $node->title->value; // "Lorem Ipsum..."
// Author Simple Start
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
$author_simple
=
$line
[
17
];
$author_simple
=
$line
[
17
];
preg_match_all
(
preg_match_all
(
'#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#'
,
'#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#'
,
$author_simple
,
$author_simple
,
$match
$match
);
);
// Author Simple
$social_media_url
=
$match
[
0
];
$social_media_url
=
$match
[
0
];
$strings
=
explode
(
'https://'
,
$author_simple
);
$strings
=
explode
(
'https://'
,
$author_simple
);
$name_surname
=
$strings
[
0
];
$name_surname
=
$strings
[
0
];
$name_surname
;
// Name & Surname
echo
'<pre>'
;
$social_media_url
[
0
];
// Social Media URL
echo
$name_surname
;
if
(
$node_exists
==
true
)
{
echo
'</pre>'
;
$node_storage
=
\Drupal
::
entityTypeManager
()
->
getStorage
(
'node'
echo
'<pre>'
;
);
echo
$social_media_url
[
0
];
$node
=
$node_storage
->
load
(
$nid
);
echo
'</pre>'
;
$entity
=
Node
::
load
(
$nid
);
$file_id
=
$line
[
16
];
$block_content
=
BlockContent
::
create
([
$file
=
\Drupal\file\Entity\File
::
load
(
$file_id
);
'type'
=>
'message'
,
$uri
=
$file
->
getFileUri
();
'info'
=>
'Demo Block1'
,
$url
=
file_create_url
(
$uri
);
'reusable'
=>
'0'
,
'body'
=>
[
echo
'<pre>'
;
[
echo
$url
;
'value'
=>
'This is the block content1'
,
echo
'</pre>'
;
'format'
=>
filter_default_format
(),
],
$media
=
Media
::
load
(
$line
[
16
]);
'reusable'
=>
'0'
,
],
echo
'<pre>'
;
]);
print_r
(
$media
);
$block_content
->
set
(
'body'
,
$name_surname
);
echo
'</pre>'
;
$block_content
->
save
();
$block_ids
[
'first'
]
=
$block_content
->
getRevisionId
();
echo
'------------------'
;
// new line
$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>'
;
echo
'<pre>'
;
print_r
(
$line
);
print_r
(
$line
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment