class Breakpoint

Defines the Breakpoint entity.

Plugin annotation


@Plugin(
  id = "breakpoint",
  label = @Translation("Breakpoint"),
  module = "breakpoint",
  controller_class = "Drupal\Core\Config\Entity\ConfigStorageController",
  config_prefix = "breakpoint.breakpoint",
  entity_keys = {
    "id" = "id",
    "label" = "label",
    "uuid" = "uuid"
  }
)

Hierarchy

Expanded class hierarchy of Breakpoint

10 files declare their use of Breakpoint
breakpoint.module in drupal/core/modules/breakpoint/breakpoint.module
Manage breakpoints and breakpoint groups for responsive designs.
BreakpointAPITest.php in drupal/core/modules/breakpoint/lib/Drupal/breakpoint/Tests/BreakpointAPITest.php
Definition of Drupal\breakpoint\Tests\BreakpointAPITest.
BreakpointCRUDTest.php in drupal/core/modules/breakpoint/lib/Drupal/breakpoint/Tests/BreakpointCRUDTest.php
Definition of Drupal\breakpoint\Tests\BreakpointCRUDTest.
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.

... See full list

6 string references to 'Breakpoint'
BreakpointAPITest::getInfo in drupal/core/modules/breakpoint/lib/Drupal/breakpoint/Tests/BreakpointAPITest.php
BreakpointCRUDTest::getInfo in drupal/core/modules/breakpoint/lib/Drupal/breakpoint/Tests/BreakpointCRUDTest.php
BreakpointGroupAPITest::getInfo in drupal/core/modules/breakpoint/lib/Drupal/breakpoint/Tests/BreakpointGroupAPITest.php
BreakpointGroupCRUDTest::getInfo in drupal/core/modules/breakpoint/lib/Drupal/breakpoint/Tests/BreakpointGroupCRUDTest.php
BreakpointMediaQueryTest::getInfo in drupal/core/modules/breakpoint/lib/Drupal/breakpoint/Tests/BreakpointMediaQueryTest.php

... See full list

File

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

Namespace

Drupal\breakpoint\Plugin\Core\Entity
View source
class Breakpoint extends ConfigEntityBase {

  /**
   * Denotes that a breakpoint or breakpoint group is defined by a theme.
   */
  const SOURCE_TYPE_THEME = 'theme';

  /**
   * Denotes that a breakpoint or breakpoint group is defined by a module.
   */
  const SOURCE_TYPE_MODULE = 'module';

  /**
   * Denotes that a breakpoint or breakpoint group is defined by the user.
   */
  const SOURCE_TYPE_USER_DEFINED = 'custom';

  /**
   * The breakpoint ID (config name).
   *
   * @var string
   */
  public $id;

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

  /**
   * The breakpoint name (machine name) as specified by theme or module.
   *
   * @var string
   */
  public $name;

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

  /**
   * The breakpoint media query.
   *
   * @var string
   */
  public $mediaQuery = '';

  /**
   * The breakpoint source.
   *
   * @var string
   */
  public $source = 'user';

  /**
   * The breakpoint source type.
   *
   * @var string
   *   Allowed values:
   *     Breakpoint::SOURCE_TYPE_THEME
   *     Breakpoint::SOURCE_TYPE_MODULE
   *     Breakpoint::SOURCE_TYPE_USER_DEFINED
   */
  public $sourceType = Breakpoint::SOURCE_TYPE_USER_DEFINED;

  /**
   * The breakpoint weight.
   *
   * @var weight
   */
  public $weight = 0;

  /**
   * The breakpoint multipliers.
   *
   * @var multipliers
   */
  public $multipliers = array();

  /**
   * Overrides Drupal\config\ConfigEntityBase::save().
   */
  public function save() {

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

    // Build an id if none is set.
    // Since a particular name can be used by multiple theme/modules we need
    // to make a unique id.
    if (empty($this->id)) {
      $this->id = $this->sourceType . '.' . $this->source . '.' . $this->name;
    }

    // Set the label if none is set.
    if (empty($this->label)) {
      $this->label = $this->name;
    }

    // Remove unused multipliers.
    $this->multipliers = array_filter($this->multipliers);

    // Always add '1x' multiplier, use array_key_exists since the value might
    // be NULL.
    if (!array_key_exists('1x', $this->multipliers)) {
      $this->multipliers = array(
        '1x' => '1x',
      ) + $this->multipliers;
    }
    return parent::save();
  }

  /**
   * Checks if the breakpoint is valid.
   *
   * @throws Drupal\breakpoint\InvalidBreakpointSourceTypeException
   * @throws Drupal\breakpoint\InvalidBreakpointSourceException
   * @throws Drupal\breakpoint\InvalidBreakpointNameException
   * @throws Drupal\breakpoint\InvalidBreakpointMediaQueryException
   *
   * @see isValidMediaQuery()
   */
  public function isValid() {

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

    // Check for illegal characters in breakpoint names.
    if (preg_match('/[^0-9a-z_\\-]/', $this->name)) {
      throw new InvalidBreakpointNameException(format_string("Invalid value '@name' for breakpoint name property. Breakpoint name property can only contain lowercase alphanumeric characters, underscores (_), and hyphens (-).", array(
        '@name' => $this->name,
      )));
    }
    return $this::isValidMediaQuery($this->mediaQuery);
  }

  /**
   * Checks if a mediaQuery is valid.
   *
   * @throws Drupal\breakpoint\InvalidBreakpointMediaQueryException
   *
   * @return true
   *   Returns true if the media query is valid.
   *
   * @see http://www.w3.org/TR/css3-mediaqueries/
   * @see http://www.w3.org/Style/CSS/Test/MediaQueries/20120229/reports/implement-report.html
   * @see https://github.com/adobe/webkit/blob/master/Source/WebCore/css/
   */
  public static function isValidMediaQuery($media_query) {

    // Array describing all known media features and the expected value type or
    // an array containing the allowed values.
    $media_features = array(
      'width' => 'length',
      'min-width' => 'length',
      'max-width' => 'length',
      'height' => 'length',
      'min-height' => 'length',
      'max-height' => 'length',
      'device-width' => 'length',
      'min-device-width' => 'length',
      'max-device-width' => 'length',
      'device-height' => 'length',
      'min-device-height' => 'length',
      'max-device-height' => 'length',
      'orientation' => array(
        'portrait',
        'landscape',
      ),
      'aspect-ratio' => 'ratio',
      'min-aspect-ratio' => 'ratio',
      'max-aspect-ratio' => 'ratio',
      'device-aspect-ratio' => 'ratio',
      'min-device-aspect-ratio' => 'ratio',
      'max-device-aspect-ratio' => 'ratio',
      'color' => 'integer',
      'min-color' => 'integer',
      'max-color' => 'integer',
      'color-index' => 'integer',
      'min-color-index' => 'integer',
      'max-color-index' => 'integer',
      'monochrome' => 'integer',
      'min-monochrome' => 'integer',
      'max-monochrome' => 'integer',
      'resolution' => 'resolution',
      'min-resolution' => 'resolution',
      'max-resolution' => 'resolution',
      'scan' => array(
        'progressive',
        'interlace',
      ),
      'grid' => 'integer',
    );
    if ($media_query) {

      // Strip new lines and trim.
      $media_query = str_replace(array(
        "\r",
        "\n",
      ), ' ', trim($media_query));

      // Remove comments /* ... */.
      $media_query = preg_replace('/\\/\\*[\\s\\S]*?\\*\\//', '', $media_query);

      // Check media list.
      $parts = explode(',', $media_query);
      foreach ($parts as $part) {

        // Split on ' and '
        $query_parts = explode(' and ', trim($part));
        $media_type_found = FALSE;
        foreach ($query_parts as $query_part) {
          $matches = array();

          // Try to match: '(media_feature: value)' and variants.
          if (preg_match('/^\\(([\\w\\-]+)(:\\s?([\\w\\-\\.]+))?\\)/', trim($query_part), $matches)) {

            // Single expression like '(color)'.
            if (isset($matches[1]) && !isset($matches[2])) {
              if (!array_key_exists($matches[1], $media_features)) {
                throw new InvalidBreakpointMediaQueryException('Invalid media feature detected.');
              }
            }
            elseif (isset($matches[3]) && !isset($matches[4])) {
              $value = trim($matches[3]);
              if (!array_key_exists($matches[1], $media_features)) {

                // We need to allow vendor prefixed media features and make sure
                // we are future proof, so only check allowed characters.
                if (!preg_match('/^[a-zA-Z0-9\\:\\-\\ ]+$/i', trim($matches[1]))) {
                  throw new InvalidBreakpointMediaQueryException('Invalid media query detected.');
                }
              }
              elseif (is_array($media_features[$matches[1]])) {

                // Check if value is allowed.
                if (!array_key_exists($value, $media_features[$matches[1]])) {
                  throw new InvalidBreakpointMediaQueryException('Value is not allowed.');
                }
              }
              elseif (isset($media_features[$matches[1]])) {
                switch ($media_features[$matches[1]]) {
                  case 'length':
                    $length_matches = array();

                    // Check for a valid number and an allowed unit.
                    if (preg_match('/^(\\-)?(\\d+(?:\\.\\d+)?)?((?:|em|ex|px|cm|mm|in|pt|pc|deg|rad|grad|ms|s|hz|khz|dpi|dpcm))$/i', trim($value), $length_matches)) {

                      // Only -0 is allowed.
                      if ($length_matches[1] === '-' && $length_matches[2] !== '0') {
                        throw new InvalidBreakpointMediaQueryException('Invalid length detected.');
                      }

                      // If there's a unit, a number is needed as well.
                      if ($length_matches[2] === '' && $length_matches[3] !== '') {
                        throw new InvalidBreakpointMediaQueryException('Unit found, value is missing.');
                      }
                    }
                    else {
                      throw new InvalidBreakpointMediaQueryException('Invalid unit detected.');
                    }
                    break;
                }
              }
            }
          }
          elseif (preg_match('/^((?:only|not)?\\s?)([\\w\\-]+)$/i', trim($query_part), $matches)) {
            if ($media_type_found) {
              throw new InvalidBreakpointMediaQueryException('Only one media type is allowed.');
            }
            $media_type_found = TRUE;
          }
          elseif (preg_match('/^((?:only|not)\\s?)\\(([\\w\\-]+)\\)$/i', trim($query_part), $matches)) {
            throw new InvalidBreakpointMediaQueryException('Invalid media query detected.');
          }
          else {

            // We need to allow vendor prefixed media fetures and make sure we
            // are future proof, so only check allowed characters.
            if (!preg_match('/^[a-zA-Z0-9\\-\\ ]+$/i', trim($query_part), $matches)) {
              throw new InvalidBreakpointMediaQueryException('Invalid media query detected.');
            }
          }
        }
      }
      return TRUE;
    }
    throw new InvalidBreakpointMediaQueryException('Media query is empty.');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Breakpoint::$id public property The breakpoint ID (config name).
Breakpoint::$label public property The breakpoint label.
Breakpoint::$mediaQuery public property The breakpoint media query.
Breakpoint::$multipliers public property The breakpoint multipliers.
Breakpoint::$name public property The breakpoint name (machine name) as specified by theme or module.
Breakpoint::$source public property The breakpoint source.
Breakpoint::$sourceType public property The breakpoint source type.
Breakpoint::$uuid public property The breakpoint UUID.
Breakpoint::$weight public property The breakpoint weight.
Breakpoint::isValid public function Checks if the breakpoint is valid.
Breakpoint::isValidMediaQuery public static function Checks if a mediaQuery is valid.
Breakpoint::save public function Overrides Drupal\config\ConfigEntityBase::save(). Overrides Entity::save
Breakpoint::SOURCE_TYPE_MODULE constant Denotes that a breakpoint or breakpoint group is defined by a module.
Breakpoint::SOURCE_TYPE_THEME constant Denotes that a breakpoint or breakpoint group is defined by a theme.
Breakpoint::SOURCE_TYPE_USER_DEFINED constant Denotes that a breakpoint or breakpoint group is defined by the user.
ConfigEntityBase::$originalID protected property The original ID of the configuration entity.
ConfigEntityBase::createDuplicate public function Overrides Entity::createDuplicate(). Overrides Entity::createDuplicate 1
ConfigEntityBase::get public function Overrides Entity::get(). Overrides Entity::get 1
ConfigEntityBase::getOriginalID public function Implements ConfigEntityInterface::getOriginalID(). Overrides ConfigEntityInterface::getOriginalID
ConfigEntityBase::isNew final 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::sort public static function Helper callback for uasort() to sort configuration entities by weight and label.
ConfigEntityBase::__construct public function Overrides Entity::__construct(). Overrides Entity::__construct 2
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. 4
Entity::$newRevision protected property Boolean indicating whether a new revision should be created on save.
Entity::access public function Implements AccessibleInterface::access(). Overrides AccessibleInterface::access
Entity::bundle public function Implements EntityInterface::bundle(). Overrides EntityInterface::bundle 4
Entity::delete public function Implements EntityInterface::delete(). Overrides EntityInterface::delete
Entity::enforceIsNew public function Implements EntityInterface::enforceIsNew(). Overrides EntityInterface::enforceIsNew
Entity::entityInfo public function Implements EntityInterface::entityInfo(). Overrides EntityInterface::entityInfo
Entity::entityType public function Implements EntityInterface::entityType(). Overrides EntityInterface::entityType
Entity::getIterator public function Implements ComplexDataInterface::getIterator(). 1
Entity::getProperties public function Implements ComplexDataInterface::getProperties(). Overrides ComplexDataInterface::getProperties 1
Entity::getPropertyDefinition public function Implements ComplexDataInterface::getPropertyDefinition(). Overrides ComplexDataInterface::getPropertyDefinition 1
Entity::getPropertyDefinitions public function Implements ComplexDataInterface::getPropertyDefinitions(). Overrides ComplexDataInterface::getPropertyDefinitions 1
Entity::getPropertyValues public function Implements ComplexDataInterface::getPropertyValues(). Overrides ComplexDataInterface::getPropertyValues 1
Entity::getRevisionId public function Implements Drupal\Core\Entity\EntityInterface::getRevisionId(). Overrides EntityInterface::getRevisionId 3
Entity::getTranslation public function Implements TranslatableInterface::getTranslation(). Overrides TranslatableInterface::getTranslation 1
Entity::getTranslationLanguages public function Implements TranslatableInterface::getTranslationLanguages(). Overrides TranslatableInterface::getTranslationLanguages 1
Entity::id public function Implements EntityInterface::id(). Overrides EntityInterface::id 10
Entity::isDefaultRevision public function Implements Drupal\Core\Entity\EntityInterface::isDefaultRevision(). Overrides EntityInterface::isDefaultRevision 1
Entity::isEmpty public function Implements ComplexDataInterface::isEmpty(). Overrides ComplexDataInterface::isEmpty 1
Entity::isNewRevision public function Implements EntityInterface::isNewRevision(). Overrides EntityInterface::isNewRevision
Entity::label public function Implements EntityInterface::label(). Overrides EntityInterface::label 1
Entity::language public function Implements TranslatableInterface::language(). Overrides TranslatableInterface::language 1
Entity::setNewRevision public function Implements EntityInterface::setNewRevision(). Overrides EntityInterface::setNewRevision
Entity::setPropertyValues public function Implements ComplexDataInterface::setPropertyValues(). Overrides ComplexDataInterface::setPropertyValues 1
Entity::translations public function Returns the languages the entity is translated to.
Entity::uri public function Implements EntityInterface::uri(). Overrides EntityInterface::uri 1
Entity::uuid public function Implements EntityInterface::uuid(). Overrides EntityInterface::uuid 1