You are here

class TocManager in TOC API 8

Defines a service that creates and manages table of contents instances.

Hierarchy

Expanded class hierarchy of TocManager

1 string reference to 'TocManager'
toc_api.services.yml in ./toc_api.services.yml
toc_api.services.yml
1 service uses TocManager
toc_api.manager in ./toc_api.services.yml
Drupal\toc_api\TocManager

File

src/TocManager.php, line 12

Namespace

Drupal\toc_api
View source
class TocManager implements TocManagerInterface {

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * The TOC instances.
   *
   * @var \Drupal\toc_api\Toc[]
   */
  protected $tocs = [];

  /**
   * Constructs a new TocManager.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   */
  public function __construct(ConfigFactoryInterface $config_factory) {
    $this->configFactory = $config_factory;
  }

  /**
   * {@inheritdoc}
   */
  public function create($id, $source, array $options = []) {

    // Merge default TOC type options with passed options.

    /** @var \Drupal\toc_api\TocTypeInterface $default_toc */
    if ($default_toc = TocType::load('default')) {
      $options = NestedArray::mergeDeep($default_toc
        ->getOptions(), $options);
    }

    // Create and store a reference to a new Toc.
    $this->tocs[$id] = new Toc($source, $options);
    return $this->tocs[$id];
  }

  /**
   * {@inheritdoc}
   */
  public function getToc($id) {
    return isset($this->tocs[$id]) ? $this->tocs[$id] : NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function reset($id = NULL) {
    if ($id === NULL) {
      $this->tocs = [];
    }
    else {
      unset($this->tocs[$id]);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TocManager::$configFactory protected property The config factory.
TocManager::$tocs protected property The TOC instances.
TocManager::create public function Constructs a new TOC object. Overrides TocManagerInterface::create
TocManager::getToc public function Get the current TOC instance. Overrides TocManagerInterface::getToc
TocManager::reset public function Reset the current TOC instance. Overrides TocManagerInterface::reset
TocManager::__construct public function Constructs a new TocManager.