diff --git a/src/Form/HumanstxtSettingsForm.php b/src/Form/HumansTxtAdminSettingsForm.php similarity index 51% rename from src/Form/HumanstxtSettingsForm.php rename to src/Form/HumansTxtAdminSettingsForm.php index f9436a8..e7bc77a 100644 --- a/src/Form/HumanstxtSettingsForm.php +++ b/src/Form/HumansTxtAdminSettingsForm.php @@ -4,55 +4,18 @@ namespace Drupal\humanstxt\Form; use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Form\FormStateInterface; -use Drupal\Core\Logger\LoggerChannelFactoryInterface; -use Drupal\Core\Messenger\MessengerInterface; -use Exception; -use Symfony\Component\DependencyInjection\ContainerInterface; +use Drupal\Core\Cache\Cache; /** - * Implements the Humanstxt Settings form controller. - * - * This Form is the Config basic form for the Humans.txt module. + * Class HumanstxtAdminSettingsForm. + * HumanstxtAdminSettingsForm implements the Humanstxt Settings Form Builder. + * Returns a Form as the Config basic form for the Humans.txt module. * + * @package Drupal\humanstxt\Form + * @access public * @see \Drupal\Core\Form\ConfigFormBase */ -class HumanstxtSettingsForm extends ConfigFormBase { - - /** - * The messenger. - * - * @var \Drupal\Core\Messenger\MessengerInterface - */ - protected $messenger; - - /** - * The logger. - * - * @var \Drupal\Core\Logger\LoggerChannelFactoryInterface - */ - protected $logger; - - /** - * Constructs a HumanstxtSettingsForm instance. - * - * @param \Drupal\Core\Messenger\MessengerInterface $messenger - * The messenger. - * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger - * The logger factory. - */ - public function __construct(MessengerInterface $messenger, - LoggerChannelFactoryInterface $logger) { - $this->messenger = $messenger; - $this->logger = $logger; - } - - public static function create(ContainerInterface $container) { - return new static( - $container->get('messenger'), - $container->get('logger.factory') - ); - } - +class HumansTxtAdminSettingsForm extends ConfigFormBase { /** * Getter method for Form ID. @@ -141,13 +104,6 @@ class HumanstxtSettingsForm extends ConfigFormBase { return parent::buildForm($form, $form_state); } - /** - * {@inheritdoc} - */ - public function validateForm(array &$form, FormStateInterface $form_state) { - parent::validateForm($form, $form_state); - } - /** * {@inheritdoc} */ @@ -163,60 +119,9 @@ class HumanstxtSettingsForm extends ConfigFormBase { ->set('display_link', $display_link_submit) ->save(); - // Call to the file processing function. - $this->humanstxt_processing_file($content_submit); - + // Delete former cache tags. + Cache::invalidateTags(['humanstxt']); parent::submitForm($form, $form_state); } - /** - * Helper function to process the Humans.txt File. - */ - function humanstxt_processing_file($content) { - try { - - // Set the complete path for the file. - $file_path = DRUPAL_ROOT . '/humans.txt'; - - // Getting the file with -w- option truncate the file to zero length. - // If the file does not exist attempt to create it. - $humanstxt_open = fopen($file_path, 'w'); - if ( $humanstxt_open === FALSE) { - - // Failed processing the file. - throw new Exception("Could not open the file!"); - } elseif (fwrite($humanstxt_open, $content) === FALSE) { - - // Failed writing the file. - throw new Exception("Could not write the file!"); - } elseif (fclose($humanstxt_open) === FALSE) { - - // Failed closing the file. - throw new Exception("Could not close the file!"); - } - - }catch (Exception $e) { - - // If an error occurred we cannot save the file as linked in checkbox. - // Set the value in the config object of the module. - $this->config('humanstxt.settings') - ->set('display_link', 0) - ->save(); - - // Load the error message in two places. - $this->messenger->addError(t("It was not possible to process the - file, due to problems in permissions. - Your current User in system is: @user. - Please review the permissions or change the - current user in the project folder." - , ['@user' => get_current_user()])); - - $this->logger->get('humanstxt')->error(t('Unable to write in root - folder. Review the permissions or - change the user owner (chmod, - chown). ' . $e->getMessage())); - - } - } - }