You are here

public function BreakpointGroup::addBreakpointFromMediaQuery in Breakpoints 8

Add a breakpoint using a name and a media query.

Parameters

string $name: The name of the breakpoint.

string $media_query: Media query.

File

lib/Drupal/breakpoint/BreakpointGroup.php, line 220
Definition of Drupal\breakpoint\BreakpointGroup.

Class

BreakpointGroup
Defines the BreakpointGroup entity.

Namespace

Drupal\breakpoint

Code

public function addBreakpointFromMediaQuery($name, $media_query) {

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

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

    // Reset name, label, weight, overridden and media query.
    $breakpoint->name = $name;
    $breakpoint->label = drupal_ucfirst($name);
    $breakpoint->mediaQuery = $media_query;
    $breakpoint->originalMediaQuery = '';
    $breakpoint->overridden = FALSE;
    $breakpoint->weight = count($this->breakpoints);
    $breakpoint
      ->save();
  }
  $this->breakpoints[$breakpoint
    ->id()] = $breakpoint;
}