As per Wikipedia Data Migration is the process of transferring data from one system to another while changing the storage, database or application. In reference to the ETL (Extract-Transform-Load) process, data migration always requires at least Extract and Load steps.
Like Migrating Data from Drupal 7 database to Drupal 8 database. We can perform Migration on file systems.
Migration can also happen to same (Drupal 7 to Drupal 7) or in between two other systems.
Where Upgrade is the process of replacing your existing software with a newer version of the same product. Like Upgrading your site from Drupal 7 to Drupal 8 version.
Here We'll focus on Migration of NODE or Content Type. Before we start reading about the NODE migration process we must be familiar with some basic things.
Please be sure that character sets (encoding) are set as same of Migration Source Data and Migration Destination Data like UTF-8.
Migration basically happens with three major steps i.e analyzing source data, prepare destination data structure and the process stays in between.
Say we are developing a module as Custom Migration module with Module key as "migrate_custom".
And yes we'll require to develop this for Drupal 8 :) by maintaining its standard.
migrate_custom will have some dependencies with other modules like Migrate (migrate), Migrate Plus (migrate_plus), Migrate Tools (migrate_tools), Migrate Drupal ...
Lets start with Files Migration with Code Example:
Our aim is to migrate all records from d7.file_managed to d8.file_managed through a command like
$ drush migrate-import custom_file --limit="500 items"
Ad per Drupal 8 standard we need a yml file to define everything and a php file to process that yml.
Here is there file/ folder structure
migrate_custom\src\Plugin\migrate\source\File.php
migrate_custom\config\install\migrate_plus.migration.custom_file.yml
In .yml file we define four major parts. These are id, source, process and destination. Id is to identify this Migration process. Source is defined with the data source. Process define basically the mapping of fields in between destination and source.
id: custom_file
source:
plugin: custom_file
process:
fid: fid
filename: filename
uri: uri
uid:
-
plugin: default_value
default_value: 1
destination:
plugin: entity:file
status: true
migration_group: custom
dependencies:
module:
- file
label: Migrate D7 Files
migration_dependencies: {}
Inside File.php class File extends SqlBase { } with four basic public functions these are
1. query() to fetch data from the source.
2. fields() to defined needed fields.
3. getIds() to get and return fid of File.
4. prepareRow() is basically to set source Property. Here we are settings uri.
A working copy of this Migration is available at Github.
Looking for a TYPO3 or Drupal or mean.io or React expert? Reach me at nirmalya.mondal@gmail.com
Tuesday, November 29, 2016
Sunday, November 20, 2016
Drupal 8: Show content based on website visitor's country or content localization/ personalization.
Sometimes we need localization of contents i.e if a website visitor is browsing site from USA will be shown different content then someone is browsing from India or Germany.
So our aim is to show Content based on Visitor's Country, Geo IP, Geo Location.
To do so we'll extend the required content type with a filed say Country. While creating a new content there it will appear a Select Box to choose country as an Option. If this content is set to a country then it'll be available for that country only otherwise it'll be available for all countries.
Here is the technical guidance towards the solution.
Drupal Modules required
For example i've created one content type as "Country Based Content" with fields Title, Body, Country.
Where Country is field type Country.
Also created some contents of the above content type.
Created a view as "country" to show this content type in the Front-end of the website through view Module.
Its very easy but still i am attaching a screen-shot for beginners.
Usage of GeoIP Module :
GeoIP Module tells Drupal about the Country location of the visitor of the site based on Visitor’s
IP Address.
To explain it more technically,
GeoIP Module register a service as “geoip.geolocation” via its Constructor by following Drupal
Service registration standard.
We need to trace the website visitor’s IP v4 address through our PHP code and have to pass
this IP Address to GeoIP Module.
We use this code to trace the IP Address.
$visitorIp = \Drupal::request()->getClientIp();
As per my previous state we need to call the service:
\Drupal::service('geoip.geolocation')->geolocate('14.140.162.242');
So here is the output below:
CommerceGuys\Intl\Country\Country Object
(
[countryCode:protected] => IN
[name:protected] => India
[threeLetterCode:protected] => IND
[numericCode:protected] => 356
[currencyCode:protected] => INR
[locale:protected] => en
)
Now we’ll access property of Country Object from its Data Model.
We need to get Country Code value from countryCode” property. So this code below will return
“IN” as my country code.
\Drupal::service('geoip.geolocation')->geolocate($visitorIp)->getCountryCode();
localization of Content for Website Visitors:
As i am browsing this site from from India so it’ll display only contents belong to India.
Let’s explain a little bit in coding level.
To alter the view we’ve used hook_views_query_alter()
Where we’ve traced the view id to alter.
function geoip_content_views_query_alter(Drupal\views\ViewExecutable $view,
Drupal\views\Plugin\views\query\Sql $query) {
switch ($view->id()) {
case 'country':
$data = 'IN';
$query->where[2]['conditions'][1]['value'] = [$data];
break;
}
}
So our aim is to show Content based on Visitor's Country, Geo IP, Geo Location.
To do so we'll extend the required content type with a filed say Country. While creating a new content there it will appear a Select Box to choose country as an Option. If this content is set to a country then it'll be available for that country only otherwise it'll be available for all countries.
Here is the technical guidance towards the solution.
Drupal Modules required
- Country
- Composer Manager
- Address
- GeoIP
For example i've created one content type as "Country Based Content" with fields Title, Body, Country.
Where Country is field type Country.
Also created some contents of the above content type.
Created a view as "country" to show this content type in the Front-end of the website through view Module.
Its very easy but still i am attaching a screen-shot for beginners.
Usage of GeoIP Module :
GeoIP Module tells Drupal about the Country location of the visitor of the site based on Visitor’s
IP Address.
To explain it more technically,
GeoIP Module register a service as “geoip.geolocation” via its Constructor by following Drupal
Service registration standard.
We need to trace the website visitor’s IP v4 address through our PHP code and have to pass
this IP Address to GeoIP Module.
We use this code to trace the IP Address.
$visitorIp = \Drupal::request()->getClientIp();
As per my previous state we need to call the service:
\Drupal::service('geoip.geolocation')->geolocate('14.140.162.242');
So here is the output below:
CommerceGuys\Intl\Country\Country Object
(
[countryCode:protected] => IN
[name:protected] => India
[threeLetterCode:protected] => IND
[numericCode:protected] => 356
[currencyCode:protected] => INR
[locale:protected] => en
)
Now we’ll access property of Country Object from its Data Model.
We need to get Country Code value from countryCode” property. So this code below will return
“IN” as my country code.
\Drupal::service('geoip.geolocation')->geolocate($visitorIp)->getCountryCode();
localization of Content for Website Visitors:
As i am browsing this site from from India so it’ll display only contents belong to India.
Let’s explain a little bit in coding level.
To alter the view we’ve used hook_views_query_alter()
Where we’ve traced the view id to alter.
function geoip_content_views_query_alter(Drupal\views\ViewExecutable $view,
Drupal\views\Plugin\views\query\Sql $query) {
switch ($view->id()) {
case 'country':
$data = 'IN';
$query->where[2]['conditions'][1]['value'] = [$data];
break;
}
}
Subscribe to:
Posts (Atom)