You are here

class WidgetManager in Fivestar 8

Contains methods for managing votes.

Hierarchy

Expanded class hierarchy of WidgetManager

2 files declare their use of WidgetManager
FivestarFormatterBase.php in src/Plugin/Field/FieldFormatter/FivestarFormatterBase.php
FivestarWidgetBase.php in src/Plugin/Field/FieldWidget/FivestarWidgetBase.php
1 string reference to 'WidgetManager'
fivestar.services.yml in ./fivestar.services.yml
fivestar.services.yml
1 service uses WidgetManager
fivestar.widget_manager in ./fivestar.services.yml
Drupal\fivestar\WidgetManager

File

src/WidgetManager.php, line 10

Namespace

Drupal\fivestar
View source
class WidgetManager {

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * Constructs a new VoteManager object.
   *
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   */
  public function __construct(ModuleHandlerInterface $module_handler) {
    $this->moduleHandler = $module_handler;
  }

  /**
   * Returns an array of all widgets.
   *
   * @return array
   *   An associative array of widgets and their info.
   *
   * @see hook_fivestar_widgets()
   */
  public function getWidgets() {
    $widgets =& drupal_static(__FUNCTION__);
    if (isset($widgets)) {
      return $widgets;
    }
    $widgets = $this->moduleHandler
      ->invokeAll('fivestar_widgets');

    // Invoke hook_fivestar_widgets_alter() to allow all modules to alter the
    // discovered widgets.
    $this->moduleHandler
      ->alter('fivestar_widgets', $widgets);
    return $widgets;
  }

  /**
   * Returns a widget's info.
   *
   * @param string $widget_key
   *   The key of the target widget.
   *
   * @return array
   *   An array of widget info.
   *
   * @see hook_fivestar_widgets()
   */
  public function getWidgetInfo($widget_key) {
    $widgets_info = $this
      ->getWidgets();
    return isset($widgets_info[$widget_key]) ? $widgets_info[$widget_key] : [];
  }

  /**
   * Returns the label for a given widget if it exists.
   *
   * @param string $widget_key
   *   The key of the target widget.
   *
   * @return string
   *   The widget label.
   */
  public function getWidgetLabel($widget_key) {
    if (!($widget_info = $this
      ->getWidgetInfo($widget_key))) {
      return '';
    }
    return $widget_info['label'];
  }

  /**
   * Returns the library for a given widget if it exists.
   *
   * @param string $widget_key
   *   The key of the target widget.
   *
   * @return string
   *   The library name.
   */
  public function getWidgetLibrary($widget_key) {
    if (!($widget_info = $this
      ->getWidgetInfo($widget_key))) {
      return '';
    }
    return $widget_info['library'];
  }

  /**
   * Returns an array of field options based on available widgets.
   *
   * @return array
   *   Associative array where the key is the option value and the value is the
   *   option label.
   */
  public function getWidgetsOptionSet() {
    $options = [];
    foreach ($this
      ->getWidgets() as $widget_key => $widget_info) {
      $options[$widget_key] = $widget_info['label'];
    }
    return $options;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
WidgetManager::$moduleHandler protected property The module handler.
WidgetManager::getWidgetInfo public function Returns a widget's info.
WidgetManager::getWidgetLabel public function Returns the label for a given widget if it exists.
WidgetManager::getWidgetLibrary public function Returns the library for a given widget if it exists.
WidgetManager::getWidgets public function Returns an array of all widgets.
WidgetManager::getWidgetsOptionSet public function Returns an array of field options based on available widgets.
WidgetManager::__construct public function Constructs a new VoteManager object.