Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions modules/contacts_demo/contacts_demo.info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: Contacts Demo
type: module
description: 'Generate demo content for contacts.'
package: Contacts
core: '8.x'
dependencies:
- contacts
5 changes: 5 additions & 0 deletions modules/contacts_demo/contacts_demo.links.menu.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
contacts_demo.generate:
title: 'Generate demo contacts'
description: 'Generate and remove demo content for the contacts module.'
route_name: contacts_demo.generate
parent: 'system.admin_config_development'
9 changes: 9 additions & 0 deletions modules/contacts_demo/contacts_demo.links.task.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
contacts_demo.generate:
route_name: contacts_demo.generate
base_route: contacts_demo.generate
title: 'Generate'

contacts_demo.generate_delete:
route_name: contacts_demo.generate_delete
base_route: contacts_demo.generate
title: 'Delete'
15 changes: 15 additions & 0 deletions modules/contacts_demo/contacts_demo.routing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
contacts_demo.generate:
path: '/admin/config/development/contacts-demo'
defaults:
_form: '\Drupal\contacts_demo\Form\GenerateForm'
_title: 'Generate Demo Contacts'
requirements:
_permission: 'administer site configuration'

contacts_demo.generate_delete:
path: '/admin/config/development/contacts-demo/delete'
defaults:
_form: '\Drupal\contacts_demo\Form\GenerateDeleteForm'
_title: 'Delete Demo Contacts'
requirements:
_permission: 'administer site configuration'
10 changes: 10 additions & 0 deletions modules/contacts_demo/contacts_demo.services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
services:
contacts_demo.yaml_parser:
class: \Drupal\contacts_demo\DemoContentParser
arguments: ~
plugin.manager.demo_content:
class: \Drupal\contacts_demo\DemoContentManager
parent: default_plugin_manager
plugin.manager.demo_flavour:
class: Drupal\contacts_demo\DemoFlavourManager
arguments: ['@module_handler']
22 changes: 22 additions & 0 deletions modules/contacts_demo/content/entity/file.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
636d6ded-c1fd-47c4-b43a-02054a0ba17d:
uuid: 636d6ded-c1fd-47c4-b43a-02054a0ba17d
langcode: en
path: content/files/file79KuSX.jpg
uri: public://file79KuSX.jpg
filemime: image/jpeg
status: 1
71bf39b6-868b-4d7b-a76e-ee34534ddd82:
uuid: 71bf39b6-868b-4d7b-a76e-ee34534ddd82
langcode: en
path: content/files/fileAEmLXi.jpg
uri: public://fileAEmLXi.jpg
filemime: image/jpeg
status: 1
d06145bb-e63c-474a-9326-99f44a6e6936:
uuid: aa51ee1e-dee0-11e5-b86d-9a79f06e9478
langcode: en
path: content/files/fileaZZVtU.jpg
uri: public://fileaZZVtU.jpg
filemime: image/jpeg
status: 1
43 changes: 43 additions & 0 deletions modules/contacts_demo/content/entity/user.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
75d9689e-3227-4f16-a5bf-5a179f70978d:
uuid: 75d9689e-3227-4f16-a5bf-5a179f70978d
mail: susanna.ramirez@example.com
user_name: blackrabbit442
decoupled: 1
status: 1
picture: 636d6ded-c1fd-47c4-b43a-02054a0ba17d
roles:
- crm_indiv
full_name: "Mrs Susanna Ramirez"
gender: female
dob: 1979-03-18
phone: "07000 142124"
address: "7645 Mill Road|Portsmouth|J0T 2SA|United Kingdom"
ec2d9ca0-6e85-4023-b056-6368e865f274:
uuid: ec2d9ca0-6e85-4023-b056-6368e865f274
mail: rich.rhodes@example.com
user_name:
decoupled: 0
status: 1
picture: 71bf39b6-868b-4d7b-a76e-ee34534ddd82
roles:
- crm_indiv
full_name: "Mr Rich Rhodes"
gender: male
dob: 1986-10-22
phone: "07000 832892"
address: "7034 The Grove|Carlisle|D74 8XW|United Kingdom"
283ce822-c4c8-4f5e-9b44-a87c5228cf25:
uuid: 283ce822-c4c8-4f5e-9b44-a87c5228cf25
mail: eleanor.mccoy@example.com
user_name:
decoupled: 0
status: 1
picture: d06145bb-e63c-474a-9326-99f44a6e6936
roles:
- crm_indiv
full_name: "Miss Eleanor Mccoy"
gender: female
dob: 1964-09-27
phone: "07000 844870"
address: "4295 Station Road|Sunderland|M19 7GX|United Kingdom"
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions modules/contacts_demo/flavours/complex.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
id: complex
label: Complex
description: "Complex contacts manifest for simple demo."
supported: false
9 changes: 9 additions & 0 deletions modules/contacts_demo/flavours/default.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
id: default
label: Default
description: "Default contacts manifest for simple demo."
supported: true
files:
user:
- content/entity/user.yml
file:
- content/entity/file.yml
42 changes: 42 additions & 0 deletions modules/contacts_demo/src/Annotation/DemoContent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Drupal\contacts_demo\Annotation;

use Drupal\Component\Annotation\Plugin;

/**
* Defines a DemoContent annotation object.
*
* @Annotation
*/
class DemoContent extends Plugin {

/**
* The plugin ID.
*
* @var string
*/
public $id;

/**
* The content type label.
*
* @var string
*/
public $label;

/**
* The source file.
*
* @var string
*/
public $source;

/**
* The entity type id.
*
* @var string
*/
public $entityType;

}
144 changes: 144 additions & 0 deletions modules/contacts_demo/src/DemoContent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?php

namespace Drupal\contacts_demo;

use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Plugin\PluginBase;

abstract class DemoContent extends PluginBase implements DemoContentInterface {

/**
* Contains the created content.
*
* @var array
*/
protected $content = [];

/**
* Contains data from a file.
*
* @var array
*/
protected $data = [];

/**
* Contains source files.
*
* @var array
*/
protected $source = [];

/**
* Parser.
*
* @var \Drupal\contacts_demo\DemoContentParserInterface
*/
protected $parser;

/**
* Contains the entity storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $entityStorage;

/**
* {@inheritdoc}
*/
public function getSource() {
return $this->source;
}

/**
* {@inheritdoc}
*/
public function getModule() {
$definition = $this->getPluginDefinition();
return isset($definition['provider']) ? $definition['provider'] : NULL;
}

/**
* {@inheritdoc}
*/
public function removeContent() {
$data = $this->fetchData();

foreach ($data as $uuid => $item) {
// Must have uuid and same key value.
if ($uuid !== $item['uuid']) {
continue;
}

$entities = $this->entityStorage->loadByProperties([
'uuid' => $uuid,
]);

foreach ($entities as $entity) {
$entity->delete();
}
}
}

/**
* {@inheritdoc}
*/
public function count() {
return count($this->content);
}

/**
* {@inheritdoc}
*/
public function setEntityStorage(EntityStorageInterface $entity_storage) {
$this->entityStorage = $entity_storage;
}

/**
* {@inheritdoc}
*/
public function setSource($source) {
$this->source = $source;
}

/**
* Gets the data from a file.
*/
protected function fetchData() {
if (!$this->data) {
foreach ($this->getSource() as $source) {
$this->data = array_merge($this->data, $this->parser->parseFile($source, $this->getModule()));
}
}
return $this->data;
}

/**
* Load entity by uuid.
*
* @param string $entity_type_id
* @param string $uuid
* @param bool $all
* @return \Drupal\Core\Entity\EntityInterface|\Drupal\Core\Entity\EntityInterface[]|mixed
*/
protected function loadByUuid($entity_type_id, $uuid, $all = FALSE) {
$storage = \Drupal::entityTypeManager()->getStorage($entity_type_id);
$entities = $storage->loadByProperties([
'uuid' => $uuid,
]);

if (!$all) {
return current($entities);
}

return $entities;
}

/**
* Makes an array with data of an entity.
*
* @param array $item
* @return array
*/
abstract protected function getEntry($item);

}
61 changes: 61 additions & 0 deletions modules/contacts_demo/src/DemoContentInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Drupal\contacts_demo;

use Drupal\Component\Plugin\PluginInspectionInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Entity\EntityStorageInterface;

interface DemoContentInterface extends PluginInspectionInterface, ContainerFactoryPluginInterface {

/**
* Returns the file name.
*
* @return string
* The source filename where are data.
*/
public function getSource();

/**
* Returns the module name.
*
* @return string
* The module name where is placed file with data.
*/
public function getModule();

/**
* Creates content.
*
* @return array
* An array with list of created entities.
*/
public function createContent();

/**
* Removes content.
*/
public function removeContent();

/**
* Returns quantity of created items.
*
* @return int
*/
public function count();

/**
* Set source array.
*
* @param array $source
*/
public function setSource($source);

/**
* Set entity storage.
*
* @param \Drupal\Core\Entity\EntityStorageInterface $entity_storage
*/
public function setEntityStorage(EntityStorageInterface $entity_storage);

}
Loading