/rif_imports

a Drupal8 extension module for importing RIF's hike into Drupal

Primary LanguagePHPGNU General Public License v2.0GPL-2.0

Drupal 8 Module

Target/History

orgin of the need

Ressources

Other Topics

Inserting the way without batch

$fids = Drupal::entityQuery('file')
->condition('status', FILE_STATUS_PERMANENT, '<>')
->condition('changed', REQUEST_TIME - $age, '<')
->range(0, 100)
->execute();

Complementary issues:

How I imported the custom content type from an existing installation:

Things to remember:

  • Along the way there are many things I have to remember that is why I need a TODOs List

Running the project:

install a brand new Drupal 8 in french

install a global Drush

Activate the rif_imports module into your fresh Drupal installation

  • The rif_imports project has to be downloaded into your $DRUPAL_HOME/modules

    • on way to do this is via the administrative interface (Download zip)

    • another way is to use a git command:

    • To active the just downloaded rif_imports module, just run the following command drush --root=$DRUPAL_HOME en -y rif_imports

  • At the end we should have passed the following commands:

jpmena@jpmena-HP ~/RIF $ export DRUPAL_HOME=$HOME/RIF/d8rif
#we clone the project in our drupal module directory
jpmena@jpmena-HP ~/RIF $ cd $DRUPAL_HOME/modules
jpmena@jpmena-HP ~/RIF/d8rif/modules $ git clone https://github.com/javaskater/rif_imports.git
Clonage dans 'rif_imports'...
remote: Counting objects: 196, done.
remote: Total 196 (delta 0), reused 0 (delta 0), pack-reused 196
Réception d\'objets: 100% (196/196), 79.11 KiB | 0 bytes/s, fait.
Résolution des deltas: 100% (93/93), fait.
Vérification de la connectivité... fait.
jpmena@jpmena-HP ~/RIF/d8rif/modules $ cd -
/home/jpmena/RIF
  • Activating $DRUPAL_HOME/modules/rif_imports module via the global drush:
#we activate the module
jpmena@jpmena-HP ~/RIF $ drush --root=$DRUPAL_HOME en -y rif_imports
The following projects have unmet dependencies:                                                                                                     [ok]
rif_imports requires delete_all
Would you like to download them? (y/n): y
Project delete_all (8.x-1.0-alpha1) downloaded to /home/jpmena/RIF/d8rif//modules/delete_all.                                                       [success]
The following extensions will be enabled: rif_imports, delete_all, serialization, rest, randonnee_de_journee
Do you really want to continue? (y/n): y
delete_all was enabled successfully.                                                                                                                [ok]
randonnee_de_journee was enabled successfully.                                                                                                      [ok]
rest was enabled successfully.                                                                                                                      [ok]
rest defines the following permissions: restful delete entity:node, restful get entity:node, restful patch entity:node, restful post entity:node
rif_imports was enabled successfully.                                                                                                               [ok]
serialization was enabled successfully.                                                                                                             [ok]

enabling the rif_imports module has added the Randonnée de Jour Custom Type and its views

  • rif_imports activation activated also the embedded randonnee_de_journee module (module previously created by feature on a first working drupal 8 installation)

    • so we don't have to activate rif_imports/dependencies/custom/randonnee_de_journee by ourselves:
      • as the directory randonnee_de_journee is already under the module directory, drush has no problem seing it and activating it as a dependant module like rest for example...
    • rif_imports/dependencies/custom/randonnee_de_journee module has itself a lot of dependent modules!
      • the Drupal8 rest modules are automatically downloaded and then activated as the custom type randonnee_de_journee had been initially defined with a rest services' view (a view for web's rest services).
  • NB : If rif_imports/dependencies/custom/randonnee_de_journee was not automatically activated as dependent rif_imports dependant module we would have had to pass the following command:

jpmena@jpmena-HP ~/RIF $ drush --root=$DRUPAL_HOME en -y randonnee_de_journee
The following extensions will be enabled: randonnee_de_journee, serialization, rest
Do you really want to continue? (y/n): y
randonnee_de_journee was enabled successfully.                                                                                                      [ok]
rest was enabled successfully.                                                                                                                      [ok]
rest defines the following permissions: restful delete entity:node, restful get entity:node, restful patch entity:node, restful post entity:node
serialization was enabled successfully.                                                                                                             [ok]

Running the SHELL

I want to import the DayHikes exported from ACCESS through a csv file

  • We just hav to run the first drush command exported by the rif_imports module which is:
    • defined underneath is the corresponding extract of $DRUPAL_HOME/modules/rif_imports/drush/rif_imports.drush.inc
function rif_imports_drush_command() {
    $items = array();

    $items['rif-import-randos-jours'] = array(
        'description' => 'Imports Days Hikes from a RIF csv File  into Drupal (Drupal 8 Rando-Journee content type)',
        'options' => array(
            'csv' => 'path of the csv file to import the day hikes from ...',
        ),
        'examples' => array(
            'drush rif-import-randos-jours csv="~/RIF/importations/randonnees.csv"' => 'import day hikes from the specified csv file',
        ),
        'aliases' => array('rirj'),
    );
  • So we will use the rirj drush command together with the path to the csv file containing the Day's Hikes to be imported:
    • the module comes with sample csv files, so we will use those ones for this example
jpmena@jpmena-HP ~/RIF $ export DRUPAL_HOME=$HOME/RIF/d8rif
jpmena@jpmena-HP ~/RIF $ drush --root=$DRUPAL_HOME rirj --csv=$DRUPAL_HOME/modules/rif_imports/examples/csvfiles/randonnees.sample.csv
  • randonnees.sample.csv has only 2 lines... So I imported only two day-hikes!!!

I want to remove the DayHikes that ACCESS exported in the csv file enfing with eff.csv

  • We just hav to run the second drush command exported by the rif_imports module which is :
    • defined underneath is the corresponding extract of $DRUPAL_HOME/modules/rif_imports/drush/rif_imports.drush.inc
function rif_imports_drush_command() {
    $items = array();

    ................................

    $items['rif-efface-randos-jours'] = array(
    'description' => 'Remove Days Hikes (Drupal 8 Rando-Journee content type) contained in the csv file from Drupal',
    'options' => array(
        'csv' => 'path of the csv file where the day hikes to remove are specified ...',
    ),
    'examples' => array(
        'drush rif-efface-randos-jours csv="~/RIF/importations/randonnees.eff.csv"' => 'remove day hikes specified the csv file',
    ),
    'aliases' => array('rerj'),
);
  • So we will use the rerj drush command with the path to the csv file containing the Day's Hikes to be imported:
    • e stands for efface which is the french translation of remove !!!
    • the module comes with sample csv files, so we will use those ones for this example
jpmena@jpmena-HP ~/RIF $ export DRUPAL_HOME=$HOME/RIF/d8rif
jpmena@jpmena-HP ~/RIF $ drush --root=$DRUPAL_HOME rerj --csv=$DRUPAL_HOME/modules/rif_imports/examples/csvfiles/randonnees.sample.eff.csv
from 2 dayhikes to remove, 1 effectively removed !!!
  • randonnees.sample.eff.csv has only 2 lines... So I rmoved only the day-hike the has a cle equal to 31018 !!!

enabling the rif_imports module has also downloaded and enabled the delete_all module

  • I decided to define the delete_all module as a dependency of my rif_imports module (like for randonnee_de_journee)!
    • the content of module's definition file (rif_imports/rif_imports.info.yml) is in fact
type: module
name: Imports RIF's Hikes insite Drupal
description: Update the RIF's Drupal with the csv exports of the RIF's ACCESS Database
package: Development
# core: '8.x'

# Information added by Drupal.org packaging script on 2016-05-16
version: '8.x-0.1-alpha1'
core: '8.x'
project: 'rif-imports'

#the content type definition is one of the dependencies (relative path dependencies/custom/randonnee_de_journee)
## I need the content type randonnee_de_journee to be defined before importing the corresponding entities
#delete_all is another dependency as it allow me to delete the node of type Randonnée de Journée
## through the command: drush @d8rif.dev dadc randonnee_de_journee
dependencies:
  - randonnee_de_journee
  - delete_all
  • Target of the delete_all module:

    • empty all the users
    • empty all the content of a custom entity type!
  • I want to empty all the content of type randonnee_de_journee (reinitiate my RIF's Drupal8 with fresh content):

    • to that purpose, I only have to pass the following comand:
    jpmena@jpmena-HP ~/RIF $ drush --root=$DRUPAL_HOME dadc --type randonnee_de_journee
    Deleting node with nid 1                                                                                                                            [ok]
    Deleting node with nid 2                                                                                                                            [ok]
    Deleted 2 nodes.                                                                                                                                    [status]
  • As I previously imported only 2 dayhikes, there are now only 2 dayhikes to be removed (2 nodes deleted)