diff --git a/progresscirclebar.info.yml b/progresscirclebar.info.yml index e1998b9ea9e2d70ae91b9c167a4f05b1c9aeb820..324681f37e1624c0245663af8ef00d39aade1edb 100644 --- a/progresscirclebar.info.yml +++ b/progresscirclebar.info.yml @@ -2,4 +2,3 @@ name: Progress Circle OR Bar type: module description: "Creates Beautiful Progress Circles or Bar" core_version_requirement: ^8 || ^9 || ^10 -version: VERSION diff --git a/progresscirclebar.libraries.yml b/progresscirclebar.libraries.yml index db313a856b58a9c9b80877960ea44e521d40f43d..55cbf9dc08f76687f1567b48e3f2dd40ac29f269 100644 --- a/progresscirclebar.libraries.yml +++ b/progresscirclebar.libraries.yml @@ -11,4 +11,4 @@ bar: component: assets/css/progress-bar.css: {} js: - assets/js/progress-bar.js: {} \ No newline at end of file + assets/js/progress-bar.js: {} diff --git a/src/Form/ProgressSettingsForm.php b/src/Form/ProgressSettingsForm.php index e175209c6844e4710239f77243390d4ec2dcf69d..f48791dc123d85dc36bfeb98f3707c79ac40cdb6 100644 --- a/src/Form/ProgressSettingsForm.php +++ b/src/Form/ProgressSettingsForm.php @@ -4,11 +4,46 @@ namespace Drupal\progresscirclebar\Form; use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Form\FormStateInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; +use Drupal\Core\Config\ConfigFactoryInterface; +use Drupal\Core\Messenger\MessengerInterface; +use Drupal\Core\StringTranslation\StringTranslationTrait; /** * Progress Settings Form. */ class ProgressSettingsForm extends ConfigFormBase { + use StringTranslationTrait; + + /** + * The messenger. + * + * @var \Drupal\Core\Messenger\MessengerInterface + */ + protected $messenger; + + /** + * Constructs a ProgressSettingsForm object. + * + * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory + * The factory for configuration objects. + * @param \Drupal\Core\Messenger\MessengerInterface $messenger + * The messenger service. + */ + public function __construct(ConfigFactoryInterface $config_factory, MessengerInterface $messenger) { + parent::__construct($config_factory); + $this->messenger = $messenger; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('config.factory'), + $container->get('messenger'), + ); + } /** * {@inheritdoc} @@ -32,40 +67,41 @@ class ProgressSettingsForm extends ConfigFormBase { $default_circle_val_color = '#00ff00'; $default_bar_wrapper_color = '#1A2C34'; $default_bar_val_color = '#00ff00'; + $settings = $this->config('progress.settings'); - if (!empty(\Drupal::config('progress.settings')->get('progress_circle_wrapper'))) { - $default_circle_wrapper_color = \Drupal::config('progress.settings')->get('progress_circle_wrapper'); + if (!empty($settings->get('progress_circle_wrapper'))) { + $default_circle_wrapper_color = $settings->get('progress_circle_wrapper'); } - if (!empty(\Drupal::config('progress.settings')->get('progress_circle_value'))) { - $default_circle_val_color = \Drupal::config('progress.settings')->get('progress_circle_value'); + if (!empty($settings->get('progress_circle_value'))) { + $default_circle_val_color = $settings->get('progress_circle_value'); } - if (!empty(\Drupal::config('progress.settings')->get('progress_bar_wrapper'))) { - $default_bar_wrapper_color = \Drupal::config('progress.settings')->get('progress_bar_wrapper'); + if (!empty($settings->get('progress_bar_wrapper'))) { + $default_bar_wrapper_color = $settings->get('progress_bar_wrapper'); } - if (!empty(\Drupal::config('progress.settings')->get('progress_bar_value'))) { - $default_bar_val_color = \Drupal::config('progress.settings')->get('progress_bar_value'); + if (!empty($settings->get('progress_bar_value'))) { + $default_bar_val_color = $settings->get('progress_bar_value'); } $form['progress']['progress_circle_wrapper'] = [ - '#title' => t("Progress Circle Wrapper Color"), + '#title' => $this->t("Progress Circle Wrapper Color"), '#type' => 'textfield', '#default_value' => $default_circle_wrapper_color, ]; $form['progress']['progress_circle_value'] = [ - '#title' => t("Progress Circle Value Color"), + '#title' => $this->t("Progress Circle Value Color"), '#type' => 'textfield', '#default_value' => $default_circle_val_color, ]; $form['progress']['progress_bar_wrapper'] = [ - '#title' => t("Progress Bar Wrapper Color"), + '#title' => $this->t("Progress Bar Wrapper Color"), '#type' => 'textfield', '#default_value' => $default_bar_wrapper_color, ]; $form['progress']['progress_bar_value'] = [ - '#title' => t("Progress Bar Value Color"), + '#title' => $this->t("Progress Bar Value Color"), '#type' => 'textfield', '#default_value' => $default_bar_val_color, ]; @@ -82,12 +118,12 @@ class ProgressSettingsForm extends ConfigFormBase { $progress_bar_value = $form_state->getValue('progress_bar_value'); // Load configuration object and save values. - $config = \Drupal::getContainer()->get('config.factory')->getEditable('progress.settings'); + $config = $this->config('progress.settings'); $config->set('progress_circle_wrapper', $progress_circle_wrapper)->save(); $config->set('progress_circle_value', $progress_circle_value)->save(); $config->set('progress_bar_wrapper', $progress_bar_wrapper)->save(); $config->set('progress_bar_value', $progress_bar_value)->save(); - \Drupal::messenger()->addMessage('The settings have been saved!'); + $this->messenger->addMessage($this->t('The settings have been saved!')); } } diff --git a/src/Plugin/Field/FieldFormatter/ProgressBarFormatter.php b/src/Plugin/Field/FieldFormatter/ProgressBarFormatter.php index 6add0a78b1e7ecb67e06511722e1a2b52ff6cea3..3d625433b682dcc100cd27ec126ccae109ee553d 100644 --- a/src/Plugin/Field/FieldFormatter/ProgressBarFormatter.php +++ b/src/Plugin/Field/FieldFormatter/ProgressBarFormatter.php @@ -4,6 +4,9 @@ namespace Drupal\progresscirclebar\Plugin\Field\FieldFormatter; use Drupal\Core\Field\FormatterBase; use Drupal\Core\Field\FieldItemListInterface; +use Drupal\Core\Config\ConfigFactoryInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; +use Drupal\Core\Field\FieldDefinitionInterface; /** * Plugin implementation of the 'field_progress_bar' formatter. @@ -20,6 +23,65 @@ use Drupal\Core\Field\FieldItemListInterface; */ class ProgressBarFormatter extends FormatterBase { + /** + * Drupal\Core\Config\ConfigFactoryInterface definition. + * + * @var \Drupal\Core\Config\ConfigFactoryInterface + */ + protected $config; + + /** + * Constructs a new ProgressCircleFormatter object. + * + * @param string $plugin_id + * The plugin_id for the formatter. + * @param mixed $plugin_definition + * The plugin implementation definition. + * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition + * The definition of the field to which the formatter is associated. + * @param array $settings + * The formatter settings. + * @param string $label + * The formatter label display setting. + * @param string $view_mode + * The view mode. + * @param array $third_party_settings + * Any third party settings. + * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory + * The config factory. + */ + public function __construct( + $plugin_id, + $plugin_definition, + FieldDefinitionInterface $field_definition, + array $settings, + $label, + $view_mode, + array $third_party_settings, + ConfigFactoryInterface $config_factory) { + parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings); + $this->config = $config_factory->get('progress.settings'); + } + + /** + * {@inheritdoc} + */ + public static function create( + ContainerInterface $container, + array $configuration, + $plugin_id, + $plugin_definition) { + return new static( + $plugin_id, + $plugin_definition, + $configuration['field_definition'], + $configuration['settings'], + $configuration['label'], + $configuration['view_mode'], + $configuration['third_party_settings'], + $container->get('config.factory')); + } + /** * {@inheritdoc} */ @@ -27,11 +89,11 @@ class ProgressBarFormatter extends FormatterBase { $elements = []; $bar_val_color = '#00ff00'; $bar_wrapper_color = '#1A2C34'; - if (!empty(\Drupal::config('progress.settings')->get('progress_bar_wrapper'))) { - $bar_wrapper_color = \Drupal::config('progress.settings')->get('progress_bar_wrapper'); + if (!empty($this->config->get('progress_bar_wrapper'))) { + $bar_wrapper_color = $this->config->get('progress_bar_wrapper'); } - if (!empty(\Drupal::config('progress.settings')->get('progress_bar_value'))) { - $bar_val_color = \Drupal::config('progress.settings')->get('progress_bar_value'); + if (!empty($this->config->get('progress_bar_value'))) { + $bar_val_color = $this->config->get('progress_bar_value'); } foreach ($items as $delta => $item) { diff --git a/src/Plugin/Field/FieldFormatter/ProgressCircleFormatter.php b/src/Plugin/Field/FieldFormatter/ProgressCircleFormatter.php index fbb78600fa667eb333625313265b2299144006b1..94d19746961735ea6c95fcc24ad66b85b0594a47 100644 --- a/src/Plugin/Field/FieldFormatter/ProgressCircleFormatter.php +++ b/src/Plugin/Field/FieldFormatter/ProgressCircleFormatter.php @@ -4,6 +4,9 @@ namespace Drupal\progresscirclebar\Plugin\Field\FieldFormatter; use Drupal\Core\Field\FormatterBase; use Drupal\Core\Field\FieldItemListInterface; +use Drupal\Core\Config\ConfigFactoryInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; +use Drupal\Core\Field\FieldDefinitionInterface; /** * Plugin implementation of the 'field_progress_circle' formatter. @@ -20,6 +23,65 @@ use Drupal\Core\Field\FieldItemListInterface; */ class ProgressCircleFormatter extends FormatterBase { + /** + * Drupal\Core\Config\ConfigFactoryInterface definition. + * + * @var \Drupal\Core\Config\ConfigFactoryInterface + */ + protected $config; + + /** + * Constructs a new ProgressCircleFormatter object. + * + * @param string $plugin_id + * The plugin_id for the formatter. + * @param mixed $plugin_definition + * The plugin implementation definition. + * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition + * The definition of the field to which the formatter is associated. + * @param array $settings + * The formatter settings. + * @param string $label + * The formatter label display setting. + * @param string $view_mode + * The view mode. + * @param array $third_party_settings + * Any third party settings. + * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory + * The config factory. + */ + public function __construct( + $plugin_id, + $plugin_definition, + FieldDefinitionInterface $field_definition, + array $settings, + $label, + $view_mode, + array $third_party_settings, + ConfigFactoryInterface $config_factory) { + parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings); + $this->config = $config_factory->get('progress.settings'); + } + + /** + * {@inheritdoc} + */ + public static function create( + ContainerInterface $container, + array $configuration, + $plugin_id, + $plugin_definition) { + return new static( + $plugin_id, + $plugin_definition, + $configuration['field_definition'], + $configuration['settings'], + $configuration['label'], + $configuration['view_mode'], + $configuration['third_party_settings'], + $container->get('config.factory')); + } + /** * {@inheritdoc} */ @@ -27,11 +89,11 @@ class ProgressCircleFormatter extends FormatterBase { $elements = []; $circle_val_color = '#00ff00'; $circle_wrapper_color = '#1A2C34'; - if (!empty(\Drupal::config('progress.settings')->get('progress_circle_wrapper'))) { - $circle_wrapper_color = \Drupal::config('progress.settings')->get('progress_circle_wrapper'); + if (!empty($this->config->get('progress_circle_wrapper'))) { + $circle_wrapper_color = $this->config->get('progress_circle_wrapper'); } - if (!empty(\Drupal::config('progress.settings')->get('progress_circle_value'))) { - $circle_val_color = \Drupal::config('progress.settings')->get('progress_circle_value'); + if (!empty($this->config->get('progress_circle_value'))) { + $circle_val_color = $this->config->get('progress_circle_value'); } foreach ($items as $delta => $item) {