You are here

class BrowserGroupList in Paragraphs Browser 8

Hierarchy

Expanded class hierarchy of BrowserGroupList

2 files declare their use of BrowserGroupList
BrowserType.php in src/Entity/BrowserType.php
GroupEditForm.php in src/Form/GroupEditForm.php
Contains \Drupal\paragraphs_browser\Form\GroupEditForm.

File

src/BrowserGroupList.php, line 12

Namespace

Drupal\paragraphs_browser
View source
class BrowserGroupList {
  protected $groups = array();

  /**
   * Returns list of paragraphs groups
   *
   * @return array
   */
  public function getGroups() {
    return $this->groups;
  }

  /**
   * @return array
   */
  public function getDisplayGroups() {
    $groups = $this->groups;
    $groups['_na'] = new BrowserGroupItem('_na', 'Other');
    return $groups;
  }

  /**
   * @param array $groups
   */
  public function setGroups($groups) {
    foreach ($groups as $group) {
      if ($group instanceof BrowserGroupItem) {
        $this
          ->setGroup($group);
      }
    }
  }

  /**
   * @param integer $id
   *
   * @return \Drupal\paragraphs_browser\BrowserGroupItem
   */
  public function getGroup($id) {
    return isset($this->groups[$id]) ? $this->groups[$id] : null;
  }

  /**
   * @param \Drupal\paragraphs_browser\BrowserGroupItem $group
   */
  public function setGroup(BrowserGroupItem $group) {
    $this->groups[$group
      ->getId()] = $group;
  }

  /**
   * Adds group to end of groups list, resets weight to heaviest.
   *
   * @param string $machine_name
   * @param string $label
   * @param integer $weight
   *
   * @return \Drupal\paragraphs_browser\BrowserGroupItem
   */
  public function addGroup($machine_name, $label, $weight = null) {
    if (is_null($weight)) {
      $weight = ($last_group = $this
        ->getLastGroup()) ? $last_group
        ->getWeight() + 1 : 0;
    }
    $this->groups[$machine_name] = new BrowserGroupItem($machine_name, $label, $weight);
    return $this->groups[$machine_name];
  }

  /**
   * Removes a group
   *
   * @param $id
   */
  public function removeGroup($id) {
    unset($this->groups[$id]);
  }

  /**
   * Gets the last group in the list
   *
   * @return \Drupal\paragraphs_browser\BrowserGroupItem
   */
  public function getLastGroup() {
    foreach ($this
      ->getGroups() as $group) {
      if (!isset($weight)) {
        $weight = $group
          ->getWeight();
        $last = $group;
      }
      elseif ($group
        ->getWeight() > $weight) {
        $weight = $group
          ->getWeight();
        $last = $group;
      }
    }
    return isset($last) ? $last : null;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BrowserGroupList::$groups protected property
BrowserGroupList::addGroup public function Adds group to end of groups list, resets weight to heaviest.
BrowserGroupList::getDisplayGroups public function
BrowserGroupList::getGroup public function
BrowserGroupList::getGroups public function Returns list of paragraphs groups
BrowserGroupList::getLastGroup public function Gets the last group in the list
BrowserGroupList::removeGroup public function Removes a group
BrowserGroupList::setGroup public function
BrowserGroupList::setGroups public function