class BreakpointGroup

Defines the BreakpointGroup entity.

Plugin annotation


@EntityType(
  id = "breakpoint_group",
  label = @Translation("Breakpoint group"),
  module = "breakpoint",
  controllers = {
    "storage" = "Drupal\Core\Config\Entity\ConfigStorageController"
  },
  config_prefix = "breakpoint.breakpoint_group",
  entity_keys = {
    "id" = "id",
    "label" = "label",
    "uuid" = "uuid"
  }
)

Hierarchy

Expanded class hierarchy of BreakpointGroup

5 files declare their use of BreakpointGroup
breakpoint.module in drupal/core/modules/breakpoint/breakpoint.module
Manage breakpoints and breakpoint groups for responsive designs.
BreakpointGroupAPITest.php in drupal/core/modules/breakpoint/lib/Drupal/breakpoint/Tests/BreakpointGroupAPITest.php
Definition of Drupal\breakpoint\Tests\BreakpointGroupAPITest.
BreakpointGroupCRUDTest.php in drupal/core/modules/breakpoint/lib/Drupal/breakpoint/Tests/BreakpointGroupCRUDTest.php
Definition of Drupal\breakpoint\Tests\BreakpointGroupCRUDTest.
BreakpointGroupTestBase.php in drupal/core/modules/breakpoint/lib/Drupal/breakpoint/Tests/BreakpointGroupTestBase.php
Definition of Drupal\breakpoint\Tests\BreakpointGroupTestBase.
BreakpointThemeTest.php in drupal/core/modules/breakpoint/lib/Drupal/breakpoint/Tests/BreakpointThemeTest.php
Definition of Drupal\breakpoint\Tests\BreakpointsThemeTest.

File

drupal/core/modules/breakpoint/lib/Drupal/breakpoint/Plugin/Core/Entity/BreakpointGroup.php, line 35
Definition of Drupal\breakpoint\Plugin\Core\Entity\BreakpointGroup.

Namespace

Drupal\breakpoint\Plugin\Core\Entity
View source
class BreakpointGroup extends ConfigEntityBase implements BreakpointGroupInterface {

  /**
   * The breakpoint group ID.
   *
   * @var string
   */
  public $id;

  /**
   * The breakpoint group UUID.
   *
   * @var string
   */
  public $uuid;

  /**
   * The breakpoint group machine name.
   *
   * @var string
   */
  public $name;

  /**
   * The breakpoint group label.
   *
   * @var string
   */
  public $label;

  /**
   * The breakpoint group breakpoints.
   *
   * @var array
   *   Array containing all breakpoints of this group.
   *
   * @see Drupal\breakpoint\Plugin\Core\Entity\Breakpoint
   */
  public $breakpoints = array();

  /**
   * The breakpoint group source: theme or module name. Use 'user' for
   * user-created groups.
   *
   * @var string
   */
  public $source = 'user';

  /**
   * The breakpoint group source type.
   *
   * @var string
   *   Allowed values:
   *     Breakpoint::SOURCE_TYPE_THEME
   *     Breakpoint::SOURCE_TYPE_MODULE
   *     Breakpoint::SOURCE_TYPE_USER_DEFINED
   *
   * @see Drupal\breakpoint\Plugin\Core\Entity\Breakpoint
   */
  public $sourceType = Breakpoint::SOURCE_TYPE_USER_DEFINED;

  /**
   * Overrides Drupal\config\ConfigEntityBase::__construct().
   */
  public function __construct(array $values, $entity_type) {
    parent::__construct($values, $entity_type);
    $this
      ->loadAllBreakpoints();
  }

  /**
   * Overrides Drupal\Core\Entity\Entity::save().
   */
  public function save() {

    // Check if everything is valid.
    if (!$this
      ->isValid()) {
      throw new InvalidBreakpointException('Invalid data detected.');
    }
    if (empty($this->id)) {
      $this->id = $this->sourceType . '.' . $this->source . '.' . $this->name;
    }

    // Only save the keys, but return the full objects.
    $this->breakpoints = array_keys($this->breakpoints);
    parent::save();
    $this
      ->loadAllBreakpoints();
  }

  /**
   * {@inheritdoc}
   */
  public function isValid() {

    // Check for illegal values in breakpoint group source type.
    if (!in_array($this->sourceType, array(
      Breakpoint::SOURCE_TYPE_USER_DEFINED,
      Breakpoint::SOURCE_TYPE_MODULE,
      Breakpoint::SOURCE_TYPE_THEME,
    ))) {
      throw new InvalidBreakpointSourceTypeException(format_string('Invalid source type @source_type', array(
        '@source_type' => $this->sourceType,
      )));
    }

    // Check for illegal characters in breakpoint group source.
    if (preg_match('/[^a-z_]+/', $this->source) || empty($this->source)) {
      throw new InvalidBreakpointSourceException(format_string("Invalid value '@source' for breakpoint group source property. Breakpoint group source property can only contain lowercase letters and underscores.", array(
        '@source' => $this->source,
      )));
    }

    // Check for illegal characters in breakpoint group name.
    if (preg_match('/[^a-z0-9_]+/', $this->name || empty($this->name))) {
      throw new InvalidBreakpointNameException(format_string("Invalid value '@name' for breakpoint group name property. Breakpoint group name property can only contain lowercase letters, numbers and underscores.", array(
        '@name' => $this->name,
      )));
    }
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function addBreakpointFromMediaQuery($name, $media_query) {

    // Use the existing breakpoint if it exists.
    $breakpoint = entity_load('breakpoint', $this->sourceType . '.' . $this->name . '.' . $name);
    if (!$breakpoint) {

      // Build a new breakpoint.
      $breakpoint = entity_create('breakpoint', array(
        'name' => $name,
        'label' => $name,
        'mediaQuery' => $media_query,
        'source' => $this->name,
        'sourceType' => $this->sourceType,
        'weight' => count($this->breakpoints),
      ));
      $breakpoint
        ->save();
    }
    $this->breakpoints[$breakpoint
      ->id()] = $breakpoint;
  }

  /**
   * {@inheritdoc}
   */
  public function addBreakpoints($breakpoints) {
    foreach ($breakpoints as $breakpoint_name) {

      // Check if breakpoint exists, assume $breakpoint_name is a machine name.
      $breakpoint = entity_load('breakpoint', $this->sourceType . '.' . $this->source . '.' . $breakpoint_name);

      // If the breakpoint doesn't exist, assume $breakpoint_name is an id.
      if (!$breakpoint) {
        $breakpoint = entity_load('breakpoint', $breakpoint_name);
      }

      // If the breakpoint doesn't exists, do not add it.
      if ($breakpoint) {

        // Add breakpoint to group.
        $this->breakpoints[$breakpoint
          ->id()] = $breakpoint;
      }
    }
  }

  /**
   * Loads all breakpoints, remove non-existing ones.
   *
   * @return array
   *   Array containing breakpoints keyed by their id.
   */
  protected function loadAllBreakpoints() {
    $breakpoints = $this->breakpoints;
    $this->breakpoints = array();
    foreach ($breakpoints as $breakpoint_id) {
      $breakpoint = breakpoint_load($breakpoint_id);
      if ($breakpoint) {
        $this->breakpoints[$breakpoint_id] = $breakpoint;
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BreakpointGroup::$breakpoints public property The breakpoint group breakpoints.
BreakpointGroup::$id public property The breakpoint group ID.
BreakpointGroup::$label public property The breakpoint group label.
BreakpointGroup::$name public property The breakpoint group machine name.
BreakpointGroup::$source public property The breakpoint group source: theme or module name. Use 'user' for user-created groups.
BreakpointGroup::$sourceType public property The breakpoint group source type.
BreakpointGroup::$uuid public property The breakpoint group UUID.
BreakpointGroup::addBreakpointFromMediaQuery public function Adds a breakpoint using a name and a media query. Overrides BreakpointGroupInterface::addBreakpointFromMediaQuery
BreakpointGroup::addBreakpoints public function Adds one or more breakpoints to this group. Overrides BreakpointGroupInterface::addBreakpoints
BreakpointGroup::isValid public function Checks if the breakpoint group is valid. Overrides BreakpointGroupInterface::isValid
BreakpointGroup::loadAllBreakpoints protected function Loads all breakpoints, remove non-existing ones.
BreakpointGroup::save public function Overrides Drupal\Core\Entity\Entity::save(). Overrides Entity::save
BreakpointGroup::__construct public function Overrides Drupal\config\ConfigEntityBase::__construct(). Overrides ConfigEntityBase::__construct
ConfigEntityBase::$originalID protected property The original ID of the configuration entity.
ConfigEntityBase::$status public property The enabled/disabled status of the configuration entity. 1
ConfigEntityBase::createDuplicate public function Overrides Entity::createDuplicate(). Overrides Entity::createDuplicate 2
ConfigEntityBase::disable public function Implements \Drupal\Core\Config\Entity\ConfigEntityInterface::disable(). Overrides ConfigEntityInterface::disable 1
ConfigEntityBase::enable public function Implements \Drupal\Core\Config\Entity\ConfigEntityInterface::enable(). Overrides ConfigEntityInterface::enable
ConfigEntityBase::get public function Overrides Entity::get(). Overrides Entity::get 2
ConfigEntityBase::getExportProperties public function Overrides \Drupal\Core\Entity\Entity::getExportProperties(). Overrides Entity::getExportProperties 9
ConfigEntityBase::getOriginalID public function Implements ConfigEntityInterface::getOriginalID(). Overrides ConfigEntityInterface::getOriginalID
ConfigEntityBase::isNew public function Overrides Entity::isNew(). Overrides Entity::isNew
ConfigEntityBase::set public function Overrides Entity::set(). Overrides Entity::set
ConfigEntityBase::setOriginalID public function Implements ConfigEntityInterface::setOriginalID(). Overrides ConfigEntityInterface::setOriginalID
ConfigEntityBase::setStatus public function Implements \Drupal\Core\Config\Entity\ConfigEntityInterface::setStatus(). Overrides ConfigEntityInterface::setStatus
ConfigEntityBase::sort public static function Helper callback for uasort() to sort configuration entities by weight and label. 2
ConfigEntityBase::status public function Implements \Drupal\Core\Config\Entity\ConfigEntityInterface::status(). Overrides ConfigEntityInterface::status 1
Entity::$enforceIsNew protected property Boolean indicating whether the entity should be forced to be new.
Entity::$entityType protected property The entity type.
Entity::$isDefaultRevision protected property Indicates whether this is the default revision. 1
Entity::$langcode public property The language code of the entity's default language. 7
Entity::$newRevision protected property Boolean indicating whether a new revision should be created on save.
Entity::access public function Implements \Drupal\Core\TypedData\AccessibleInterface::access(). Overrides AccessibleInterface::access
Entity::bundle public function Implements \Drupal\Core\Entity\EntityInterface::bundle(). Overrides EntityInterface::bundle 4
Entity::delete public function Implements \Drupal\Core\Entity\EntityInterface::delete(). Overrides EntityInterface::delete 3
Entity::enforceIsNew public function Implements \Drupal\Core\Entity\EntityInterface::enforceIsNew(). Overrides EntityInterface::enforceIsNew
Entity::entityInfo public function Implements \Drupal\Core\Entity\EntityInterface::entityInfo(). Overrides EntityInterface::entityInfo
Entity::entityType public function Implements \Drupal\Core\Entity\EntityInterface::entityType(). Overrides EntityInterface::entityType
Entity::getBCEntity public function Implements \Drupal\Core\Entity\EntityInterface::getBCEntity(). Overrides EntityInterface::getBCEntity 1
Entity::getConstraints public function Implements \Drupal\Core\TypedData\TypedDataInterface::getConstraints(). Overrides TypedDataInterface::getConstraints
Entity::getDefinition public function Implements \Drupal\Core\TypedData\TypedDataInterface::getDefinition(). Overrides TypedDataInterface::getDefinition
Entity::getIterator public function Implements \Drupal\Core\TypedData\ComplexDataInterface::getIterator(). 1
Entity::getName public function Implements \Drupal\Core\TypedData\TypedDataInterface::getName(). Overrides TypedDataInterface::getName
Entity::getNGEntity public function Implements \Drupal\Core\Entity\EntityInterface::getNGEntity(). Overrides EntityInterface::getNGEntity
Entity::getParent public function Implements \Drupal\Core\TypedData\TypedDataInterface::getParent(). Overrides TypedDataInterface::getParent
Entity::getProperties public function Implements \Drupal\Core\TypedData\ComplexDataInterface::getProperties(). Overrides ComplexDataInterface::getProperties 1
Entity::getPropertyDefinition public function Implements \Drupal\Core\TypedData\ComplexDataInterface::getPropertyDefinition(). Overrides ComplexDataInterface::getPropertyDefinition 1
Entity::getPropertyDefinitions public function Implements \Drupal\Core\TypedData\ComplexDataInterface::getPropertyDefinitions(). Overrides ComplexDataInterface::getPropertyDefinitions 1
Entity::getPropertyPath public function Implements \Drupal\Core\TypedData\TypedDataInterface::getPropertyPath(). Overrides TypedDataInterface::getPropertyPath
Entity::getPropertyValues public function Implements \Drupal\Core\TypedData\ComplexDataInterface::getPropertyValues(). Overrides ComplexDataInterface::getPropertyValues 1
Entity::getRevisionId public function Implements \Drupal\Core\Entity\EntityInterface::getRevisionId(). Overrides EntityInterface::getRevisionId 4
Entity::getRoot public function Implements \Drupal\Core\TypedData\TypedDataInterface::getRoot(). Overrides TypedDataInterface::getRoot
Entity::getString public function Implements \Drupal\Core\TypedData\TypedDataInterface::getString(). Overrides TypedDataInterface::getString
Entity::getTranslation public function Implements \Drupal\Core\TypedData\TranslatableInterface::getTranslation(). Overrides TranslatableInterface::getTranslation 1
Entity::getTranslationLanguages public function Implements \Drupal\Core\TypedData\TranslatableInterface::getTranslationLanguages(). Overrides TranslatableInterface::getTranslationLanguages 1
Entity::getType public function Implements \Drupal\Core\TypedData\TypedDataInterface::getType(). Overrides TypedDataInterface::getType 2
Entity::getValue public function Implements \Drupal\Core\TypedData\TypedDataInterface::getValue(). Overrides TypedDataInterface::getValue
Entity::id public function Implements \Drupal\Core\Entity\EntityInterface::id(). Overrides EntityInterface::id 11
Entity::isDefaultRevision public function Implements \Drupal\Core\Entity\EntityInterface::isDefaultRevision(). Overrides EntityInterface::isDefaultRevision 1
Entity::isEmpty public function Implements \Drupal\Core\TypedData\ComplexDataInterface::isEmpty(). Overrides ComplexDataInterface::isEmpty 1
Entity::isNewRevision public function Implements \Drupal\Core\Entity\EntityInterface::isNewRevision(). Overrides EntityInterface::isNewRevision
Entity::isTranslatable public function Implements \Drupal\Core\Entity\EntityInterface::isTranslatable(). Overrides EntityInterface::isTranslatable
Entity::label public function Implements \Drupal\Core\Entity\EntityInterface::label(). Overrides EntityInterface::label 4
Entity::language public function Implements \Drupal\Core\TypedData\TranslatableInterface::language(). Overrides TranslatableInterface::language 1
Entity::onChange public function Implements \Drupal\Core\TypedData\ComplexDataInterface::onChange(). Overrides ComplexDataInterface::onChange
Entity::setContext public function Implements \Drupal\Core\TypedData\TypedDataInterface::setContext(). Overrides TypedDataInterface::setContext
Entity::setNewRevision public function Implements \Drupal\Core\Entity\EntityInterface::setNewRevision(). Overrides EntityInterface::setNewRevision
Entity::setPropertyValues public function Implements \Drupal\Core\TypedData\ComplexDataInterface::setPropertyValues(). Overrides ComplexDataInterface::setPropertyValues 1
Entity::setValue public function Implements \Drupal\Core\TypedData\TypedDataInterface::setValue(). Overrides TypedDataInterface::setValue
Entity::translations public function Returns the languages the entity is translated to. 1
Entity::uri public function Implements \Drupal\Core\Entity\EntityInterface::uri(). Overrides EntityInterface::uri 8
Entity::uriRelationships public function Returns a list of URI relationships supported by this entity. Overrides EntityInterface::uriRelationships
Entity::uuid public function Implements \Drupal\Core\Entity\EntityInterface::uuid(). Overrides EntityInterface::uuid 1
Entity::validate public function Implements \Drupal\Core\TypedData\TypedDataInterface::validate(). Overrides TypedDataInterface::validate 1