Loading modules/quickchat_sync/src/Form/OperationForm.php +34 −5 Original line number Diff line number Diff line Loading @@ -14,6 +14,16 @@ use Symfony\Component\DependencyInjection\ContainerInterface; */ class OperationForm extends FormBase { /** * Maximum number of words for paragraph is 150. */ const QUICKCHAT_MAX_NUMBERS_WORDS = 150; /** * Maximum number of characters for paragraph is 1000. */ const QUICKCHAT_MAX_NUMBERS_CHARACTERS = 1000; /** * The configuration factory. * Loading Loading @@ -95,7 +105,7 @@ class OperationForm extends FormBase { $view_display = $sync[$model_id]['view_display']; $view_arguments = explode(',', $arguments); $preview = $this->getViewHtml($view_name, $view_display, $view_arguments); $preview = explode('\n', $preview); $preview = array_filter(explode('\n', $preview)); $preview = $this->generateHtmlList($preview); $form[$model_id]['update'] = [ Loading Loading @@ -229,6 +239,7 @@ class OperationForm extends FormBase { $response = $this->executeOperation('update_knowledge_base', $scenario_id, $token, $html); if ($response) { $this->messenger()->deleteAll(); if ($response->getStatusCode() == 200) { $this->messenger()->addMessage($this->t('The knowledge base has been updated on Quickchat AI.')); } Loading @@ -251,10 +262,10 @@ class OperationForm extends FormBase { if ($view = Views::getView($view_name)) { $build = $view->buildRenderable($view_display, $arguments); $html = strip_tags(\Drupal::service('renderer')->renderPlain($build)); $html = preg_replace('/\r|\n/', '', $html); $html = preg_replace('/\r|\n/', '', trim($html)); } return trim($html); return rtrim($html); } /** Loading @@ -281,11 +292,29 @@ class OperationForm extends FormBase { * @param array $values * Array containing the values to parse into a HTML list. */ private function generateHtmlList($values) { private function generateHtmlList(array $values) { $return = '<ul>'; foreach ($values as $value) { $return .= "<li>$value</li>"; $errors = ''; if (str_word_count($value) > OperationForm::QUICKCHAT_MAX_NUMBERS_WORDS) { $this->messenger()->addError($this->t('You entered @words words. Maximum number of words for paragraph is @max_words.', [ '@words' => str_word_count($value), '@max_words' => OperationForm::QUICKCHAT_MAX_NUMBERS_WORDS, ])); $errors = '❌'; } if (strlen($value) > OperationForm::QUICKCHAT_MAX_NUMBERS_CHARACTERS) { $this->messenger()->addError($this->t('You entered @characters characters. Maximum number of characters for paragraph is @max_characters.', [ '@characters' => strlen($value), '@max_characters' => OperationForm::QUICKCHAT_MAX_NUMBERS_CHARACTERS, ])); $errors = '❌'; } $return .= "<li>$errors $value</li>"; } $return .= '</ul>'; Loading Loading
modules/quickchat_sync/src/Form/OperationForm.php +34 −5 Original line number Diff line number Diff line Loading @@ -14,6 +14,16 @@ use Symfony\Component\DependencyInjection\ContainerInterface; */ class OperationForm extends FormBase { /** * Maximum number of words for paragraph is 150. */ const QUICKCHAT_MAX_NUMBERS_WORDS = 150; /** * Maximum number of characters for paragraph is 1000. */ const QUICKCHAT_MAX_NUMBERS_CHARACTERS = 1000; /** * The configuration factory. * Loading Loading @@ -95,7 +105,7 @@ class OperationForm extends FormBase { $view_display = $sync[$model_id]['view_display']; $view_arguments = explode(',', $arguments); $preview = $this->getViewHtml($view_name, $view_display, $view_arguments); $preview = explode('\n', $preview); $preview = array_filter(explode('\n', $preview)); $preview = $this->generateHtmlList($preview); $form[$model_id]['update'] = [ Loading Loading @@ -229,6 +239,7 @@ class OperationForm extends FormBase { $response = $this->executeOperation('update_knowledge_base', $scenario_id, $token, $html); if ($response) { $this->messenger()->deleteAll(); if ($response->getStatusCode() == 200) { $this->messenger()->addMessage($this->t('The knowledge base has been updated on Quickchat AI.')); } Loading @@ -251,10 +262,10 @@ class OperationForm extends FormBase { if ($view = Views::getView($view_name)) { $build = $view->buildRenderable($view_display, $arguments); $html = strip_tags(\Drupal::service('renderer')->renderPlain($build)); $html = preg_replace('/\r|\n/', '', $html); $html = preg_replace('/\r|\n/', '', trim($html)); } return trim($html); return rtrim($html); } /** Loading @@ -281,11 +292,29 @@ class OperationForm extends FormBase { * @param array $values * Array containing the values to parse into a HTML list. */ private function generateHtmlList($values) { private function generateHtmlList(array $values) { $return = '<ul>'; foreach ($values as $value) { $return .= "<li>$value</li>"; $errors = ''; if (str_word_count($value) > OperationForm::QUICKCHAT_MAX_NUMBERS_WORDS) { $this->messenger()->addError($this->t('You entered @words words. Maximum number of words for paragraph is @max_words.', [ '@words' => str_word_count($value), '@max_words' => OperationForm::QUICKCHAT_MAX_NUMBERS_WORDS, ])); $errors = '❌'; } if (strlen($value) > OperationForm::QUICKCHAT_MAX_NUMBERS_CHARACTERS) { $this->messenger()->addError($this->t('You entered @characters characters. Maximum number of characters for paragraph is @max_characters.', [ '@characters' => strlen($value), '@max_characters' => OperationForm::QUICKCHAT_MAX_NUMBERS_CHARACTERS, ])); $errors = '❌'; } $return .= "<li>$errors $value</li>"; } $return .= '</ul>'; Loading