diff --git a/modules/contacts_demo/contacts_demo.info.yml b/modules/contacts_demo/contacts_demo.info.yml new file mode 100644 index 0000000..ef7a3c1 --- /dev/null +++ b/modules/contacts_demo/contacts_demo.info.yml @@ -0,0 +1,7 @@ +name: Contacts Demo +type: module +description: 'Generate demo content for contacts.' +package: Contacts +core: '8.x' +dependencies: + - contacts \ No newline at end of file diff --git a/modules/contacts_demo/contacts_demo.links.menu.yml b/modules/contacts_demo/contacts_demo.links.menu.yml new file mode 100644 index 0000000..136efff --- /dev/null +++ b/modules/contacts_demo/contacts_demo.links.menu.yml @@ -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' diff --git a/modules/contacts_demo/contacts_demo.links.task.yml b/modules/contacts_demo/contacts_demo.links.task.yml new file mode 100644 index 0000000..6aefb68 --- /dev/null +++ b/modules/contacts_demo/contacts_demo.links.task.yml @@ -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' diff --git a/modules/contacts_demo/contacts_demo.routing.yml b/modules/contacts_demo/contacts_demo.routing.yml new file mode 100644 index 0000000..418fc2d --- /dev/null +++ b/modules/contacts_demo/contacts_demo.routing.yml @@ -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' diff --git a/modules/contacts_demo/contacts_demo.services.yml b/modules/contacts_demo/contacts_demo.services.yml new file mode 100644 index 0000000..81ecd95 --- /dev/null +++ b/modules/contacts_demo/contacts_demo.services.yml @@ -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'] diff --git a/modules/contacts_demo/content/entity/file.yml b/modules/contacts_demo/content/entity/file.yml new file mode 100644 index 0000000..ff64a21 --- /dev/null +++ b/modules/contacts_demo/content/entity/file.yml @@ -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 diff --git a/modules/contacts_demo/content/entity/user.yml b/modules/contacts_demo/content/entity/user.yml new file mode 100644 index 0000000..2bb04d4 --- /dev/null +++ b/modules/contacts_demo/content/entity/user.yml @@ -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" \ No newline at end of file diff --git a/modules/contacts_demo/content/files/file79KuSX.jpg b/modules/contacts_demo/content/files/file79KuSX.jpg new file mode 100644 index 0000000..5c3569b Binary files /dev/null and b/modules/contacts_demo/content/files/file79KuSX.jpg differ diff --git a/modules/contacts_demo/content/files/fileAEmLXi.jpg b/modules/contacts_demo/content/files/fileAEmLXi.jpg new file mode 100644 index 0000000..15e3d67 Binary files /dev/null and b/modules/contacts_demo/content/files/fileAEmLXi.jpg differ diff --git a/modules/contacts_demo/content/files/fileaZZVtU.jpg b/modules/contacts_demo/content/files/fileaZZVtU.jpg new file mode 100644 index 0000000..b6a30e2 Binary files /dev/null and b/modules/contacts_demo/content/files/fileaZZVtU.jpg differ diff --git a/modules/contacts_demo/flavours/complex.yml b/modules/contacts_demo/flavours/complex.yml new file mode 100644 index 0000000..715a589 --- /dev/null +++ b/modules/contacts_demo/flavours/complex.yml @@ -0,0 +1,4 @@ +id: complex +label: Complex +description: "Complex contacts manifest for simple demo." +supported: false diff --git a/modules/contacts_demo/flavours/default.yml b/modules/contacts_demo/flavours/default.yml new file mode 100644 index 0000000..61e89d5 --- /dev/null +++ b/modules/contacts_demo/flavours/default.yml @@ -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 \ No newline at end of file diff --git a/modules/contacts_demo/src/Annotation/DemoContent.php b/modules/contacts_demo/src/Annotation/DemoContent.php new file mode 100644 index 0000000..71e57b9 --- /dev/null +++ b/modules/contacts_demo/src/Annotation/DemoContent.php @@ -0,0 +1,42 @@ +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); + +} diff --git a/modules/contacts_demo/src/DemoContentInterface.php b/modules/contacts_demo/src/DemoContentInterface.php new file mode 100644 index 0000000..e44a198 --- /dev/null +++ b/modules/contacts_demo/src/DemoContentInterface.php @@ -0,0 +1,61 @@ +getPluginDefinition(); + $storage = \Drupal::entityTypeManager()->getStorage($definition['entity_type']); + $plugin->setEntityStorage($storage); +// dpm($configuration); + if (!empty($configuration['source'])) { + $plugin->setSource($configuration['source']); + } + + return $plugin; + } + + /** + * Create multiple instances. + * + * @param array $plugin_ids + * Identifiers of plugins. + * @param array $configurations + * Array with configuration for all plugins. + * @return array + * Array with instances of the plugins. + */ + public function createInstances($plugin_ids, array $configurations = []) { + $instances = []; + + foreach ($plugin_ids as $plugin_id) { + $configuration = isset($configurations[$plugin_id]) ? $configurations[$plugin_id] : []; + $instances[$plugin_id] = static::createInstance($plugin_id, $configuration); + } + + return $instances; + } + +} diff --git a/modules/contacts_demo/src/DemoContentParser.php b/modules/contacts_demo/src/DemoContentParser.php new file mode 100644 index 0000000..1612bb4 --- /dev/null +++ b/modules/contacts_demo/src/DemoContentParser.php @@ -0,0 +1,23 @@ +parse($this->getPath($file, $module)); + } + +} diff --git a/modules/contacts_demo/src/DemoContentParserInterface.php b/modules/contacts_demo/src/DemoContentParserInterface.php new file mode 100644 index 0000000..9b20c25 --- /dev/null +++ b/modules/contacts_demo/src/DemoContentParserInterface.php @@ -0,0 +1,33 @@ +parser = $parser; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('contacts_demo.yaml_parser') + ); + } + + /** + * {@inheritdoc} + */ + public function createContent() { + $data = $this->fetchData(); + $entity_type_id = $this->entityStorage->getEntityTypeId(); + + foreach ($data as $uuid => $item) { + // Must have uuid and same key value. + if ($uuid !== $item['uuid']) { +// drush_log(dt("{$entity_type_id} with uuid: {$uuid} has a different uuid in content."), LogLevel::ERROR); + continue; + } + + // Check whether comment with same uuid already exists. + $entities = $this->entityStorage->loadByProperties([ + 'uuid' => $uuid, + ]); + + if ($entities) { +// drush_log(dt("{$entity_type_id} with uuid: {$uuid} already exists."), LogLevel::WARNING); + continue; + } + + $entry = $this->getEntry($item); + $entity = $this->entityStorage->create($entry); + $entity->save(); + + if ($entity->id()) { + $this->content[ $entity->id() ] = $entity; + } + } + } + + /** + * {@inheritdoc} + */ + protected function getEntry($item) { + $entry = [ + 'uuid' => $item['uuid'], + ]; + + return $entry; + } + +} diff --git a/modules/contacts_demo/src/DemoFile.php b/modules/contacts_demo/src/DemoFile.php new file mode 100644 index 0000000..d422cc1 --- /dev/null +++ b/modules/contacts_demo/src/DemoFile.php @@ -0,0 +1,108 @@ +parser = $parser; + $this->entityTypeManager = $entity_type_manager; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('contacts_demo.yaml_parser'), + $container->get('entity_type.manager') + ); + } + + /** + * {@inheritdoc} + */ + public function createContent() { + $data = $this->fetchData(); + + foreach ($data as $uuid => $item) { + // Must have uuid and same key value. + if ($uuid !== $item['uuid']) { +// drush_log(dt("File with uuid: {$uuid} has a different uuid in content."), LogLevel::ERROR); + continue; + } + + // Check whether file with same uuid already exists. + $files = $this->entityStorage->loadByProperties([ + 'uuid' => $uuid, + ]); + + if ($files) { +// drush_log(dt("File with uuid: {$uuid} already exists."), LogLevel::WARNING); + continue; + } + + // Copy file from module. + $item['uri'] = file_unmanaged_copy( + $this->parser->getPath($item['path'], $this->getModule()), + $item['uri'], + FILE_EXISTS_REPLACE + ); + + $item['uid'] = NULL; + $entry = $this->getEntry($item); + $entity = $this->entityStorage->create($entry); + $entity->save(); + + if (!$entity->id()) { + continue; + } + + $this->content[ $entity->id() ] = $entity; + } + + return $this->content; + } + + /** + * {@inheritdoc} + */ + protected function getEntry($item) { + $entry = [ + 'uuid' => $item['uuid'], + 'langcode' => $item['langcode'], + 'uid' => $item['uid'], + 'status' => $item['status'], + 'uri' => $item['uri'], + ]; + + return $entry; + } + +} diff --git a/modules/contacts_demo/src/DemoFlavour.php b/modules/contacts_demo/src/DemoFlavour.php new file mode 100644 index 0000000..17a9e0e --- /dev/null +++ b/modules/contacts_demo/src/DemoFlavour.php @@ -0,0 +1,178 @@ +entityTypeManger = $entity_type_manager; + $this->demoContentManager = $demo_content_manager; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('entity_type.manager'), + $container->get('plugin.manager.demo_content') + ); + } + + /** + * {@inheritdoc} + */ + public function getLabel() { + return (string) $this->pluginDefinition['label']; + } + + /** + * {@inheritdoc} + */ + public function getDescription() { + return (string) $this->pluginDefinition['description']; + } + + /** + * {@inheritdoc} + */ + public function getModule() { + $definition = $this->getPluginDefinition(); + return isset($definition['provider']) ? $definition['provider'] : NULL; + } + + /** + * {@inheritdoc} + */ + public function getFiles() { + $definition = $this->getPluginDefinition(); + + $files = []; + foreach ($definition['files'] as $type => $paths) { + $files[] = [ + 'type' => $type, + 'paths' => $paths, + ]; + } + return $files; + } + + /** + * {@inheritdoc} + */ + public function count() { + // @todo count total entities in manifest. + return 10; + } + + /** + * {@inheritdoc} + */ + public function generate() { + $this->generateElements(); + drupal_set_message('Generate process complete.'); + } + + /** + * {@inheritdoc} + */ + protected function generateElements() { + if ($this->count() <= 20) { + $this->generateContent(); + } + else { + $this->generateBatchContent(); + } + } + + /** + * Method responsible for creating content when + * the number of elements is less than 50. + */ + private function generateContent() { + $num_created = 0; + + foreach ($this->getFiles() as $file) { + + $content_type = $file['type']; + $configuration = [ + 'source' => $file['paths'], + ]; + + $plugin = $this->demoContentManager->createInstance($content_type, $configuration); + + /** @var \Drupal\contacts_demo\DemoContentInterface $plugin */ + $plugin->createContent(); + + if ($file['type'] == 'user') { + $num_created += $plugin->count(); + } + } + + // Only care about number of users created. + drupal_set_message($this->formatPlural($num_created, '1 users created.', 'Finished creating @count users')); + } + + /** + * Method responsible for creating content when + * the number of elements is greater than 50. + */ + private function generateBatchContent() { + // @TODO do batch operations. + } + + /** + * {@inheritdoc} + */ + public function delete() { + foreach ($this->getFiles() as $file) { + $content_type = $file['type']; + $configuration = [ + 'source' => $file['paths'], + ]; + + /** @var \Drupal\contacts_demo\DemoContentInterface $plugin */ + $plugin = $this->demoContentManager->createInstance($content_type, $configuration); + $plugin->removeContent(); + } + drupal_set_message('Delete process complete.'); + } + +} diff --git a/modules/contacts_demo/src/DemoFlavourInterface.php b/modules/contacts_demo/src/DemoFlavourInterface.php new file mode 100644 index 0000000..0242af8 --- /dev/null +++ b/modules/contacts_demo/src/DemoFlavourInterface.php @@ -0,0 +1,61 @@ + '', + 'description' => '', + 'class' => 'Drupal\contacts_demo\DemoFlavour', + 'id' => '', + ); + + /** + * The object that discovers plugins managed by this manager. + * + * @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface + */ + protected $discovery; + + /** + * The object that instantiates plugins managed by this manager. + * + * @var \Drupal\Component\Plugin\Factory\FactoryInterface + */ + protected $factory; + + /** + * The module handler. + * + * @var \Drupal\Core\Extension\ModuleHandlerInterface + */ + protected $moduleHandler; + + + /** + * Constructs a \Drupal\contacts_demo\DemoFlavourManager object. + * + * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler + * The module handler. + */ + public function __construct(ModuleHandlerInterface $module_handler) { + $this->moduleHandler = $module_handler; + } + + /** + * {@inheritdoc} + */ + public function processDefinition(&$definition, $plugin_id) { + parent::processDefinition($definition, $plugin_id); + $definition['id'] = $plugin_id; + } + + /** + * Gets the plugin discovery. + * + * @return \Drupal\Component\Plugin\Discovery\DiscoveryInterface + */ + protected function getDiscovery() { + if (!isset($this->discovery)) { + $directories = array_map(function($directory) { + return [$directory . '/contacts_flavours', $directory . '/flavours']; + }, $this->moduleHandler->getModuleDirectories()); + + $yaml_discovery = new YamlDirectoryDiscovery($directories, 'contacts_demo'); + $this->discovery = new ContainerDerivativeDiscoveryDecorator($yaml_discovery); + } + return $this->discovery; + } + + /** + * {@inheritdoc} + */ + public function isPluginSupported($plugin_id) { + $definition = $this->getDefinition($plugin_id, FALSE); + return $definition && $definition['supported']; + } + +} diff --git a/modules/contacts_demo/src/DemoFlavourManagerInterface.php b/modules/contacts_demo/src/DemoFlavourManagerInterface.php new file mode 100644 index 0000000..9ae5bac --- /dev/null +++ b/modules/contacts_demo/src/DemoFlavourManagerInterface.php @@ -0,0 +1,23 @@ +parser = $parser; + $this->profileStorage = $profile_storage; + $this->fileStorage = $file_storage; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('contacts_demo.yaml_parser'), + $container->get('entity.manager')->getStorage('profile'), + $container->get('entity.manager')->getStorage('file') + ); + } + + /** + * {@inheritdoc} + */ + public function createContent() { + $data = $this->fetchData(); + + foreach ($data as $uuid => $item) { + // Must have uuid and same key value. + if ($uuid !== $item['uuid']) { +// drush_log(dt("User with uuid: {$uuid} has a different uuid in content."), LogLevel::ERROR); + continue; + } + + // Check whether user with same uuid already exists. + $accounts = $this->entityStorage->loadByProperties([ + 'uuid' => $uuid, + ]); + + if ($accounts) { +// drush_log(dt("User with uuid: {$uuid} already exists."), LogLevel::WARNING); + continue; + } + + // Load image by uuid and set to a profile. + if (!empty($item['picture'])) { + $item['picture'] = $this->preparePicture($item['picture']); + } + else { + // Set "null" to exclude errors during saving (in cases when picture will equal to "false"). + $item['picture'] = NULL; + } + + if ($item['decoupled']) { + $item['roles'] = array_merge($item['roles'], [AccountInterface::ANONYMOUS_ROLE]); + } + else { + $item['roles'] = array_merge($item['roles'], [AccountInterface::AUTHENTICATED_ROLE]); + } + $item['roles'] += array_filter($item['roles']); + + + $entry = $this->getEntry($item); + $account = $this->entityStorage->create($entry); + $account->enforceIsNew(); + $account->save(); + + if (!$account->id()) { + continue; + } + + $this->content[ $account->id() ] = $account; + + // Load the profile, since it's autocreated. + $profile = $this->profileStorage->create([ + 'uid' => $account->id(), + 'type' => ProfileType::load('crm_indiv')->id(), + ]); + $this->fillProfile($profile, $item); + $profile->save(); + } + + return $this->content; + } + + /** + * {@inheritdoc} + */ + protected function getEntry($item) { + $entry = [ + 'uuid' => $item['uuid'], + 'name' => $item['username'], + 'mail' => $item['mail'], + 'init' => $item['mail'], + 'status' => $item['status'], + 'user_picture' => $item['picture'], + 'created' => REQUEST_TIME, + 'changed' => REQUEST_TIME, + 'roles' => array_values($item['roles']), + ]; + + return $entry; + } + + /** + * Prepares data about an image of a profile. + * + * @param $picture + * @return array + */ + protected function preparePicture($picture) { + $value = NULL; + $files = $this->fileStorage->loadByProperties([ + 'uuid' => $picture, + ]); + + if ($files) { + $value = [ + [ + 'target_id' => current($files)->id(), + ], + ]; + } + + return $value; + } + + /** + * Fills the some fields of a profile. + * + * @param \Drupal\profile\Entity\ProfileInterface $profile + * @param array $item + */ + protected function fillProfile($profile, $item) { + $profile->crm_name = $item['full_name']; + $profile->crm_gender = $item['gender']; + $profile->crm_dob = $item['dob']; + $profile->crm_phone = $item['phone']; +// $profile->crm_address = $item['address']; + } + +} diff --git a/modules/contacts_demo/src/Form/GenerateDeleteForm.php b/modules/contacts_demo/src/Form/GenerateDeleteForm.php new file mode 100644 index 0000000..2bcc8db --- /dev/null +++ b/modules/contacts_demo/src/Form/GenerateDeleteForm.php @@ -0,0 +1,90 @@ +demoFlavourManager = $demo_flavour_manager; + $this->demoContentManager = $demo_content_manager; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('plugin.manager.demo_flavour'), + $container->get('plugin.manager.demo_content') + ); + } + + /** + * {@inheritdoc} + */ + public function getFormId() { + return 'contacts_demo_admin_generate_delete_form'; + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL) { + // @todo Show information about what content will be deleted. + $form['actions'] = array('#type' => 'actions'); + $form['actions']['submit'] = array( + '#type' => 'submit', + '#value' => $this->t('Delete'), + '#button_type' => 'primary', + ); + + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + // @todo Figure out which flavours have been generated. + $flavour = 'default'; + + /* @var $plugin DemoFlavourInterface */ + $plugin = $this->demoFlavourManager->createInstance($flavour); + // Delete content. + $plugin->delete(); + } + +} diff --git a/modules/contacts_demo/src/Form/GenerateForm.php b/modules/contacts_demo/src/Form/GenerateForm.php new file mode 100644 index 0000000..7aac9d4 --- /dev/null +++ b/modules/contacts_demo/src/Form/GenerateForm.php @@ -0,0 +1,111 @@ +demoFlavourManager = $demo_flavour_manager; + $this->demoContentManager = $demo_content_manager; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('plugin.manager.demo_flavour'), + $container->get('plugin.manager.demo_content') + ); + } + + /** + * {@inheritdoc} + */ + public function getFormId() { + return 'contacts_demo_admin_generate_form'; + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL) { + // @todo Work out if flavours have already been generated. + $form['flavours'] = array( + '#type' => 'radios', + '#title' => $this->t('Demo Flavours'), + '#required' => TRUE, + '#default_value' => 'default', + '#options' => [], + '#description' => $this->t('Select the flavour of contacts demo content you wish to install.'), + ); + + foreach ($this->demoFlavourManager->getDefinitions() as $id => $definition) { + $form['flavours']['#options'][$id] = $definition['label']; + + $supported = $this->demoFlavourManager->isPluginSupported($id); + $form['flavours'][$id]['#disabled'] = !$supported; + + $form['flavours'][$id]['#description'] = [ + '#type' => 'inline_template', + '#template' => '{{ description }}{% if not supported %}