diff --git a/modules/chatbot_api_entities/src/Entity/EntityCollection.php b/modules/chatbot_api_entities/src/Entity/EntityCollection.php index 9fdd4e2..0ba28e0 100644 --- a/modules/chatbot_api_entities/src/Entity/EntityCollection.php +++ b/modules/chatbot_api_entities/src/Entity/EntityCollection.php @@ -16,7 +16,7 @@ use Drupal\Core\Plugin\DefaultLazyPluginCollection; * label = @Translation("Entity collection"), * handlers = { * "view_builder" = "Drupal\Core\Entity\EntityViewBuilder", - * "list_builder" = "\Drupal\Core\Entity\EntityListBuilder", + * "list_builder" = "Drupal\chatbot_api_entities\EntityCollectionListBuilder", * "form" = { * "add" = "Drupal\chatbot_api_entities\Form\EntityCollectionForm", * "edit" = "Drupal\chatbot_api_entities\Form\EntityCollectionForm", @@ -126,8 +126,7 @@ class EntityCollection extends ConfigEntityBase implements EntityCollectionInter */ public function __construct(array $values, $entity_type) { parent::__construct($values, $entity_type); - $this->queryHandlerCollection = new DefaultLazyPluginCollection(\Drupal::service('plugin.manager.chatbot_api_entities_query_handler'), $this->query_handlers); - $this->pushHandlerCollection = new DefaultLazyPluginCollection(\Drupal::service('plugin.manager.chatbot_api_entities_push_handler'), $this->push_handlers); + $this->initializePluginCollections(); } /** @@ -218,6 +217,11 @@ class EntityCollection extends ConfigEntityBase implements EntityCollectionInter $this->setPushHandlerConfiguration($instance_id, $instance_configuration); } $return = parent::save(); + // Queue it for updating. + \Drupal::queue('chatbot_api_entities_push')->createItem([ + 'collection_id' => $this->id(), + 'created' => time(), + ]); return $return; } @@ -243,4 +247,26 @@ class EntityCollection extends ConfigEntityBase implements EntityCollectionInter return $this; } + /** + * {@inheritdoc} + */ + public function getSynonymField() { + return $this->synonyms; + } + + /** + * Sets up plugin collections. + */ + protected function initializePluginCollections() { + $this->queryHandlerCollection = new DefaultLazyPluginCollection(\Drupal::service('plugin.manager.chatbot_api_entities_query_handler'), $this->query_handlers); + $this->pushHandlerCollection = new DefaultLazyPluginCollection(\Drupal::service('plugin.manager.chatbot_api_entities_push_handler'), $this->push_handlers); + } + + /** + * {@inheritdoc} + */ + public function __wakeup() { + $this->initializePluginCollections(); + } + } diff --git a/modules/chatbot_api_entities/src/Entity/EntityCollectionInterface.php b/modules/chatbot_api_entities/src/Entity/EntityCollectionInterface.php index 611b554..f78a8d4 100644 --- a/modules/chatbot_api_entities/src/Entity/EntityCollectionInterface.php +++ b/modules/chatbot_api_entities/src/Entity/EntityCollectionInterface.php @@ -73,4 +73,11 @@ interface EntityCollectionInterface extends ConfigEntityInterface { */ public function setPushHandlerConfiguration($instance_id, array $configuration); + /** + * Gets the name of the synonym field. + * + * @return string + */ + public function getSynonymField(); + } diff --git a/modules/chatbot_api_entities/src/EntityCollectionListBuilder.php b/modules/chatbot_api_entities/src/EntityCollectionListBuilder.php index cf32e31..db66cc4 100644 --- a/modules/chatbot_api_entities/src/EntityCollectionListBuilder.php +++ b/modules/chatbot_api_entities/src/EntityCollectionListBuilder.php @@ -2,7 +2,26 @@ namespace Drupal\chatbot_api_entities; +use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Entity\EntityListBuilder; -class EntityCollectionListBuilder { +/** + * Defines a list builder for entity collections. + */ +class EntityCollectionListBuilder extends EntityListBuilder { + + /** + * {@inheritdoc} + */ + public function buildRow(EntityInterface $entity) { + return ['collection' => $entity->toLink($entity->label(), 'edit-form')] + parent::buildRow($entity); + } + + /** + * {@inheritdoc} + */ + public function buildHeader() { + return ['collection' => $this->t('Collection')] + parent::buildHeader(); + } } diff --git a/modules/chatbot_api_entities/src/Form/EntityCollectionForm.php b/modules/chatbot_api_entities/src/Form/EntityCollectionForm.php index 8066f14..3a447d8 100644 --- a/modules/chatbot_api_entities/src/Form/EntityCollectionForm.php +++ b/modules/chatbot_api_entities/src/Form/EntityCollectionForm.php @@ -4,14 +4,12 @@ namespace Drupal\chatbot_api_entities\Form; use Drupal\chatbot_api_entities\Plugin\PushHandlerManager; use Drupal\chatbot_api_entities\Plugin\QueryHandlerManager; +use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\Core\Entity\EntityForm; -use Drupal\Core\Entity\EntityType; use Drupal\Core\Entity\EntityTypeBundleInfoInterface; -use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\FormStateInterface; -use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -20,24 +18,39 @@ use Symfony\Component\DependencyInjection\ContainerInterface; class EntityCollectionForm extends EntityForm { /** + * Entity bundle info. + * * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface */ - private $entityTypeBundleInfo; + protected $entityTypeBundleInfo; /** + * Entity field manager. + * * @var \Drupal\Core\Entity\EntityFieldManagerInterface */ - private $entityFieldManager; + protected $entityFieldManager; /** + * Push handler manager. + * * @var \Drupal\chatbot_api_entities\Plugin\PushHandlerManager */ - private $pushHandlerManager; + protected $pushHandlerManager; /** + * Query handler manager. + * * @var \Drupal\chatbot_api_entities\Plugin\QueryHandlerManager */ - private $queryHandlerManager; + protected $queryHandlerManager; + + /** + * Entity being edited. + * + * @var \Drupal\chatbot_api_entities\Entity\EntityCollectionInterface $collection + */ + protected $entity; /** * Constructs a new EntityCollectionForm object. @@ -77,7 +90,6 @@ class EntityCollectionForm extends EntityForm { public function form(array $form, FormStateInterface $form_state) { $form = parent::form($form, $form_state); - /** @var \Drupal\chatbot_api_entities\Entity\EntityCollectionInterface $collection */ $collection = $this->entity; $form['label'] = [ '#type' => 'textfield', @@ -98,22 +110,169 @@ class EntityCollectionForm extends EntityForm { ]; $entityTypes = $this->entityTypeManager->getDefinitions(); + $options = []; + foreach ($entityTypes as $id => $entityType) { + if (!$entityType->entityClassImplements(ContentEntityInterface::class)) { + continue; + } + foreach ($this->entityTypeBundleInfo->getBundleInfo($id) as $bundleId => $bundle) { + $options[(string) $entityType->getLabel()]["$id:$bundleId"] = $bundle['label']; + } + } $form['entity_type'] = [ + '#type' => 'container', + '#attributes' => [ + 'class' => ['container-inline'], + ], + ]; + $default_entity_type = $this->getEntityTypeForCollection($form_state); + $form['entity_type']['entity_type'] = [ '#type' => 'select', - '#options' => array_combine(array_keys($entityTypes), array_map(function (EntityTypeInterface $entityType) { - return array_map(function (array $bundle_info) { - return $bundle_info['label']; - }, $this->entityTypeBundleInfo->getBundleInfo($entityType->id())); - }, $entityTypes)), + '#options' => $options, '#title' => $this->t('Entity Type'), '#description' => $this->t('Choose the entity type for the entities to comprise this collection.'), - '#default_value' => $collection->getCollectionEntityTypeId(), + '#default_value' => $default_entity_type, + '#ajax' => [ + 'wrapper' => 'edit-configuration-container', + 'callback' => '::changeEntityTypeCallback', + 'trigger_as' => [ + 'name' => 'op', + 'value' => $this->t('Change Entity type'), + ], + ], + ]; + $form['entity_type']['change'] = [ + '#limit_validation_errors' => [['entity_type']], + '#type' => 'submit', + '#value' => $this->t('Change Entity type'), + '#attributes' => [ + 'class' => ['js-hide'], + ], + '#submit' => ['::changeEntityType'], + '#ajax' => [ + 'callback' => '::changeEntityTypeCallback', + 'wrapper' => 'edit-configuration-container', + ] ]; + $fields = []; + $form['configuration'] = [ + '#type' => 'container', + '#attributes' => [ + 'id' => 'edit-configuration-container', + ], + ]; + if ($default_entity_type) { + list ($entity_type, $bundle) = explode(':', $default_entity_type, 2); + $fieldsDefinitions = $this->entityFieldManager->getFieldDefinitions($entity_type, $bundle); + foreach ($fieldsDefinitions as $name => $field) { + if ($field->getType() !== 'string') { + continue; + } + if ($field->getName() === $this->entityTypeManager->getDefinition($entity_type)->getKey('label')) { + // Bypass the title/label fields. + continue; + } + $fields[$name] = $field->getLabel(); + } + if ($fields) { + $form['configuration']['synonyms'] = [ + '#type' => 'select', + '#empty_option' => $this->t('None'), + '#empty_values' => '', + '#options' => $fields, + '#title' => $this->t('Synonyms field'), + '#description' => $this->t('Select the field to use for synonyms'), + '#default_value' => $collection->getSynonymField(), + ]; + } + else { + $form['configuration']['synonyms'] = [ + '#type' => 'markup', + '#markup' => $this->t('There are no text fields for this entity type. Add an Text (plain) field to enable synonyms support.') + ]; + } + $query_handlers = $this->queryHandlerManager->getDefinitions(); + $query_options = []; + $query_config = $collection->get('query_handlers'); + foreach ($query_handlers as $id => $query_handler) { + $instance = $this->queryHandlerManager->createInstance($id, isset($query_config[$id]) ? $query_config[$id] : []); + if ($instance->applies($entity_type)) { + $query_options[$id] = $query_handler['label']; + } + } + $form['configuration']['enabled_query_handlers'] = [ + '#type' => 'checkboxes', + '#title' => $this->t('Enabled query handlers'), + '#options' => $query_options, + '#required' => TRUE, + '#default_value' => array_keys($collection->get('query_handlers')), + ]; + } + $push_handlers = $this->pushHandlerManager->getDefinitions(); + $push_options = []; + $push_config = $collection->get('push_handlers'); + $form['enabled_push_handlers'] = [ + '#type' => 'checkboxes', + '#title' => $this->t('Enabled push handlers'), + '#required' => TRUE, + '#default_value' => array_keys($collection->get('push_handlers')), + ]; + foreach ($push_handlers as $id => $push_handler) { + $instance = $this->pushHandlerManager->createInstance($id, isset($push_config[$id]) ? $push_config[$id] : []); + $push_options[$id] = $push_handler['label']; + if ($push_form = $instance->getSettingsForm($collection, $form, $form_state)) { + $form += [ + 'push_handler_configuration' => [ + '#type' => 'vertical_tabs', + '#title' => $this->t('Push handler configuration'), + ] + ]; + $form['push_handlers']['settings'][$id] = [ + '#type' => 'details', + '#tree' => TRUE, + '#open' => TRUE, + '#title' => $push_handler['label'], + '#group' => 'push_handler_configuration', + '#parents' => ['push_handler_configuration', $id, 'settings'], + ] + $push_form; + } + } + $form['enabled_push_handlers']['#options'] = $push_options; return $form; } /** + * Submit handler. + * + * @param array $form + * Form. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * Form state. + */ + public function changeEntityType(array $form, FormStateInterface $form_state) { + $form_state->setRebuild(TRUE); + } + + /** + * Ajax callback. + * + * @param array $form + * Form. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * Form state. + * + * @return array + * Form elements. + */ + public function changeEntityTypeCallback(array $form, FormStateInterface $form_state) { + if (!isset($form['configuration'])) { + return []; + } + return $form['configuration']; + } + + /** * {@inheritdoc} */ public function save(array $form, FormStateInterface $form_state) { @@ -135,4 +294,72 @@ class EntityCollectionForm extends EntityForm { $form_state->setRedirectUrl($chatbot_api_entities_collection->toUrl('collection')); } + /** + * {@inheritdoc} + */ + public function buildEntity(array $form, FormStateInterface $form_state) { + $entity = parent::buildEntity($form, $form_state); + list ($entity_type, $bundle) = explode(':', $form_state->getValue('entity_type'), 2); + $entity->set('entity_type', $entity_type); + $entity->set('bundle', $bundle); + // Reset these to empty. + $entity->set('queryHandlers', []); + $entity->set('pushHandlers', []); + foreach (array_filter($form_state->getValue('enabled_query_handlers') ?: []) as $query_handler) { + $entity->setQueryHandlerConfiguration($query_handler, ['id' => $query_handler]); + } + foreach (array_filter($form_state->getValue('enabled_push_handlers') ?: []) as $push_handler) { + $entity->setPushHandlerConfiguration($push_handler, [ + 'id' => $push_handler, + ] + $form_state->getValue(['push_handler_configuration', $push_handler])); + } + + return $entity; + } + + /** + * {@inheritdoc} + */ + public function validateForm(array &$form, FormStateInterface $form_state) { + parent::validateForm($form, $form_state); + $push_handlers = $this->pushHandlerManager->getDefinitions(); + $push_config = $this->entity->get('pushHandlers'); + $enabled = array_filter($form_state->getValue('enabled_push_handlers')); + foreach ($push_handlers as $id => $push_handler) { + if (empty($enabled[$id])) { + continue; + } + $instance = $this->pushHandlerManager->createInstance($id, isset($push_config[$id]) ? $push_config[$id] : []); + $instance->validateSettingsForm($this->entity, $form, $form_state); + } + } + + /** + * {@inheritdoc} + */ + public function __wakeup() { + $this->entityTypeManager = \Drupal::service('entity_type.manager'); + $this->entityTypeBundleInfo = \Drupal::service('entity_type.bundle.info'); + $this->entityFieldManager = \Drupal::service('entity_field.manager'); + $this->pushHandlerManager = \Drupal::service('plugin.manager.chatbot_api_entities_push_handler'); + $this->queryHandlerManager = \Drupal::service('plugin.manager.chatbot_api_entities_query_handler'); + } + + /** + * Gets the entity type being edited. + * + * @param \Drupal\Core\Form\FormStateInterface $form_state + * Form state. + * + * @return string + * Entity type. + */ + protected function getEntityTypeForCollection(FormStateInterface $form_state) { + $default_entity_type = $form_state->getValue('entity_type'); + if (!$default_entity_type && $this->entity->getCollectionEntityTypeId()) { + $default_entity_type = $this->entity->getCollectionEntityTypeId() . ':' . $this->entity->getCollectionBundle(); + } + return $default_entity_type; + } + } diff --git a/modules/chatbot_api_entities/src/Plugin/ChatbotApiEntities/QueryHandler/DefaultEntity.php b/modules/chatbot_api_entities/src/Plugin/ChatbotApiEntities/QueryHandler/DefaultEntity.php index 90b2ed5..39c3a0e 100644 --- a/modules/chatbot_api_entities/src/Plugin/ChatbotApiEntities/QueryHandler/DefaultEntity.php +++ b/modules/chatbot_api_entities/src/Plugin/ChatbotApiEntities/QueryHandler/DefaultEntity.php @@ -46,7 +46,7 @@ class DefaultEntity extends QueryHandlerBase { * TRUE if applies. */ public function applies($entity_type_id) { - return TRUE; + return $this->pluginDefinition['entity_type'] === $entity_type_id; } /** diff --git a/modules/chatbot_api_entities/src/Plugin/ChatbotApiEntities/QueryHandler/User.php b/modules/chatbot_api_entities/src/Plugin/ChatbotApiEntities/QueryHandler/User.php index 6231d6d..8a01e2a 100644 --- a/modules/chatbot_api_entities/src/Plugin/ChatbotApiEntities/QueryHandler/User.php +++ b/modules/chatbot_api_entities/src/Plugin/ChatbotApiEntities/QueryHandler/User.php @@ -10,7 +10,7 @@ use Drupal\Core\Entity\EntityTypeManagerInterface; * * @QueryHandler( * id = "default:user", - * label = @Translation("Default - User") + * label = @Translation("User query") * ) */ class User extends DefaultEntity { diff --git a/modules/chatbot_api_entities/src/Plugin/Derivative/DefaultEntityDeriver.php b/modules/chatbot_api_entities/src/Plugin/Derivative/DefaultEntityDeriver.php index cf88148..025f2e7 100644 --- a/modules/chatbot_api_entities/src/Plugin/Derivative/DefaultEntityDeriver.php +++ b/modules/chatbot_api_entities/src/Plugin/Derivative/DefaultEntityDeriver.php @@ -50,6 +50,7 @@ class DefaultEntityDeriver extends DeriverBase implements ContainerDeriverInterf $this->derivatives[$entity_type_id] = $base_plugin_definition; $this->derivatives[$entity_type_id]['label'] = t('@entity_type query', ['@entity_type' => $entity_type->getLabel()]); $this->derivatives[$entity_type_id]['base_plugin_label'] = (string) $base_plugin_definition['label']; + $this->derivatives[$entity_type_id]['entity_type'] = $entity_type_id; } return parent::getDerivativeDefinitions($base_plugin_definition); diff --git a/modules/chatbot_api_entities/src/Plugin/PushHandlerBase.php b/modules/chatbot_api_entities/src/Plugin/PushHandlerBase.php index 65efe5b..477d95d 100644 --- a/modules/chatbot_api_entities/src/Plugin/PushHandlerBase.php +++ b/modules/chatbot_api_entities/src/Plugin/PushHandlerBase.php @@ -5,6 +5,7 @@ namespace Drupal\chatbot_api_entities\Plugin; use Drupal\chatbot_api_entities\Entity\EntityCollection; use Drupal\chatbot_api_entities\Entity\EntityCollectionInterface; use Drupal\Component\Plugin\PluginBase; +use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use GuzzleHttp\ClientInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -86,4 +87,18 @@ abstract class PushHandlerBase extends PluginBase implements PushHandlerInterfac return !empty($this->configuration['status']); } + /** + * {@inheritdoc} + */ + public function getSettingsForm(EntityCollectionInterface $entityCollection, array $form, FormStateInterface $form_state) { + return []; + } + + /** + * {@inheritdoc} + */ + public function validateSettingsForm(EntityCollectionInterface $entityCollection, array $form, FormStateInterface $form_state) { + return; + } + } diff --git a/modules/chatbot_api_entities/src/Plugin/PushHandlerInterface.php b/modules/chatbot_api_entities/src/Plugin/PushHandlerInterface.php index c0202d1..2d03022 100644 --- a/modules/chatbot_api_entities/src/Plugin/PushHandlerInterface.php +++ b/modules/chatbot_api_entities/src/Plugin/PushHandlerInterface.php @@ -5,6 +5,7 @@ namespace Drupal\chatbot_api_entities\Plugin; use Drupal\chatbot_api_entities\Entity\EntityCollection; use Drupal\chatbot_api_entities\Entity\EntityCollectionInterface; use Drupal\Component\Plugin\PluginInspectionInterface; +use Drupal\Core\Form\FormStateInterface; /** * Defines an interface for Push handler plugins. @@ -44,4 +45,31 @@ interface PushHandlerInterface extends PluginInspectionInterface { */ public function isEnabled(); + /** + * Get the settings form. + * + * @param \Drupal\chatbot_api_entities\Entity\EntityCollectionInterface $entityCollection + * Collection being edited. + * @param array $form + * The entire form. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * Form state. + * + * @return array + * Configuration form for this item. + */ + public function getSettingsForm(EntityCollectionInterface $entityCollection, array $form, FormStateInterface $form_state); + + /** + * Validate the settings form. + * + * @param \Drupal\chatbot_api_entities\Entity\EntityCollectionInterface $entityCollection + * Collection being edited. + * @param array $form + * The entire form. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * Form state. + */ + public function validateSettingsForm(EntityCollectionInterface $entityCollection, array $form, FormStateInterface $form_state); + } diff --git a/modules/chatbot_api_entities/src/Plugin/QueueWorker/ChatbotEntityPushWorker.php b/modules/chatbot_api_entities/src/Plugin/QueueWorker/ChatbotEntityPushWorker.php index aa8ea9a..5936b1b 100644 --- a/modules/chatbot_api_entities/src/Plugin/QueueWorker/ChatbotEntityPushWorker.php +++ b/modules/chatbot_api_entities/src/Plugin/QueueWorker/ChatbotEntityPushWorker.php @@ -82,6 +82,7 @@ class ChatbotEntityPushWorker extends QueueWorkerBase implements ContainerFactor if ($collection = $this->entityTypeManager->getStorage('chatbot_api_entities_collection')->load($data['collection_id'])) { $collection->queryAndPush($this->entityTypeManager); } + $this->state->set('chatbot_api_entities_last_' . $data['collection_id'], time()); } } diff --git a/modules/chatbot_api_entities/tests/modules/chatbot_api_entities_test/src/Plugin/ChatbotApiEntities/PushHandler/ChatbotApiEntitiesTestHandler.php b/modules/chatbot_api_entities/tests/modules/chatbot_api_entities_test/src/Plugin/ChatbotApiEntities/PushHandler/ChatbotApiEntitiesTestHandler.php index 64f7f68..43e3306 100644 --- a/modules/chatbot_api_entities/tests/modules/chatbot_api_entities_test/src/Plugin/ChatbotApiEntities/PushHandler/ChatbotApiEntitiesTestHandler.php +++ b/modules/chatbot_api_entities/tests/modules/chatbot_api_entities_test/src/Plugin/ChatbotApiEntities/PushHandler/ChatbotApiEntitiesTestHandler.php @@ -5,7 +5,9 @@ namespace Drupal\chatbot_api_entities_test\Plugin\ChatbotApiEntities\PushHandler use Drupal\chatbot_api_entities\Entity\EntityCollection; use Drupal\chatbot_api_entities\Entity\EntityCollectionInterface; use Drupal\chatbot_api_entities\Plugin\PushHandlerBase; +use Drupal\Core\Form\FormStateInterface; use Drupal\Core\State\StateInterface; +use Drupal\Core\StringTranslation\TranslatableMarkup; use GuzzleHttp\ClientInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -14,7 +16,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface; * * @PushHandler( * id = "chatbot_api_entities_test", - * label = @Translation("API AI entities endpoint") + * label = @Translation("Test handler") * ) */ class ChatbotApiEntitiesTestHandler extends PushHandlerBase { @@ -45,6 +47,7 @@ class ChatbotApiEntitiesTestHandler extends PushHandlerBase { public function __construct(array $configuration, $plugin_id, $plugin_definition, ClientInterface $httpClient, StateInterface $state) { parent::__construct($configuration, $plugin_id, $plugin_definition, $httpClient); $this->state = $state; + $this->configuration += ['settings' => ['remote_id' => '']]; } public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { @@ -76,4 +79,27 @@ class ChatbotApiEntitiesTestHandler extends PushHandlerBase { return parent::saveConfiguration($entityCollection, $configuration); } + /** + * {@inheritdoc} + */ + public function getSettingsForm(EntityCollectionInterface $entityCollection, array $form, FormStateInterface $form_state) { + return [ + 'remote_id' => [ + '#type' => 'textfield', + '#title' => new TranslatableMarkup('Remote name'), + '#description' => new TranslatableMarkup('Give the collection a name on the remote API'), + '#default_value' => $this->configuration['settings']['remote_id'], + ] + ]; + } + + /** + * {@inheritdoc} + */ + public function validateSettingsForm(EntityCollectionInterface $entityCollection, array $form, FormStateInterface $form_state) { + if (!$form_state->getValue(['push_handler_configuration', $this->pluginId, 'settings', 'remote_id'])) { + $form_state->setError($form['push_handlers']['settings'][$this->pluginId], new TranslatableMarkup('Remote ID is required')); + } + } + } diff --git a/modules/chatbot_api_entities/tests/src/Functional/ChatbotApiEntitiesFunctionalTest.php b/modules/chatbot_api_entities/tests/src/Functional/ChatbotApiEntitiesFunctionalTest.php index 192d107..40293d1 100644 --- a/modules/chatbot_api_entities/tests/src/Functional/ChatbotApiEntitiesFunctionalTest.php +++ b/modules/chatbot_api_entities/tests/src/Functional/ChatbotApiEntitiesFunctionalTest.php @@ -62,12 +62,20 @@ class ChatbotApiEntitiesFunctionalTest extends BrowserTestBase { 'id' => $id, 'label' => $label, 'entity_type' => 'entity_test:some_bundle', + ], 'Change Entity type'); + // Now should be a button. + $this->submitForm([ 'synonyms' => 'field_synonyms', 'enabled_query_handlers[default:entity_test]' => 1, 'enabled_push_handlers[chatbot_api_entities_test]' => 1, - 'push_handler_configuration[chatbot_api_entities_test][remote_id]' => 'Foobar', + // Intentionally blank. + 'push_handler_configuration[chatbot_api_entities_test][settings][remote_id]' => '', + ], 'Save'); + $assert->pageTextContains('Remote ID is required'); + $this->submitForm([ + 'push_handler_configuration[chatbot_api_entities_test][settings][remote_id]' => 'Foobar', ], 'Save'); - $assert->pageTextContains(t('Created the %label collection.', [ + $assert->responseContains(t('Created the %label collection.', [ '%label' => $label, ])); $this->cronRun(); @@ -76,7 +84,10 @@ class ChatbotApiEntitiesFunctionalTest extends BrowserTestBase { $asArray = $config->toArray(); $this->assertEquals([ 'chatbot_api_entities_test' => [ - 'settings' => ['remote_id' => 'Foobar'], + 'settings' => [ + 'remote_id' => 'Foobar', + 'added_at_save_time' => 'entity_test_some_bundle', + ], 'id' => 'chatbot_api_entities_test', ], ], $asArray['push_handlers']); @@ -85,6 +96,9 @@ class ChatbotApiEntitiesFunctionalTest extends BrowserTestBase { 'id' => 'default:entity_test', ], ], $asArray['query_handlers']); + $this->drupalGet($config->toUrl('edit-form')); + // Make sure defaults are shown. + $assert->fieldValueEquals('push_handler_configuration[chatbot_api_entities_test][settings][remote_id]', 'Foobar'); } } diff --git a/modules/chatbot_api_entities/tests/src/Traits/ChatbotApiEntitiesTestTrait.php b/modules/chatbot_api_entities/tests/src/Traits/ChatbotApiEntitiesTestTrait.php index 81d2b26..1f561d8 100644 --- a/modules/chatbot_api_entities/tests/src/Traits/ChatbotApiEntitiesTestTrait.php +++ b/modules/chatbot_api_entities/tests/src/Traits/ChatbotApiEntitiesTestTrait.php @@ -11,7 +11,6 @@ use Drupal\field\Entity\FieldStorageConfig; */ trait ChatbotApiEntitiesTestTrait { - /** * Creates a new entity test bundle and adds synonyms field. */ @@ -23,7 +22,7 @@ trait ChatbotApiEntitiesTestTrait { FieldStorageConfig::create([ 'field_name' => $field_name, 'entity_type' => $entity_type, - 'type' => 'text', + 'type' => 'string', 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, ])->save(); @@ -43,4 +42,5 @@ trait ChatbotApiEntitiesTestTrait { protected function cronRun() { $this->container->get('cron')->run(); } + }