You are here

class CloudBlock in Gutenberg 8

Same name and namespace in other branches
  1. 8.2 modules/gutenberg_cloud/src/CloudBlock.php \Drupal\gutenberg_cloud\CloudBlock

Class CloudBlock.

Hierarchy

Expanded class hierarchy of CloudBlock

File

modules/gutenberg_cloud/src/CloudBlock.php, line 8

Namespace

Drupal\gutenberg_cloud
View source
class CloudBlock extends CloudBlockBase {

  /**
   * Raw block retrieved from API.
   *
   * @var null
   */
  protected $raw = NULL;

  /**
   * CloudBlock constructor.
   *
   * @param mixed $block
   *   Cloud block structure.
   */
  public function __construct($block) {
    if (is_object($block)) {
      $this
        ->mapFromBlock($block);
    }
    else {
      parent::__construct($block);
    }
  }

  /**
   * Getter for raw block.
   *
   * @return object|null
   *   Block object, defaults to null.
   */
  public function getRaw() {
    return $this->raw;
  }

  /**
   * Defines prefix for accessing the assets.
   */
  public function getPrefix() {
    return $this->name . '@' . $this->version . '/';
  }

  /**
   * Get asset URL.
   *
   * @param string $name
   *   Name of the asset.
   * @param string $base_url
   *   Base Url.
   *
   * @return string|null
   *   Url of the asset or NULL if the asset name
   *   was not allowed or the name value was empty.
   */
  public function getAssetUrl($name = '', $base_url = '') {
    $allowed = [
      'screenshot',
      'js',
      'edit_css',
      'view_css',
    ];
    if (empty($name) || !in_array($name, $allowed)) {
      return NULL;
    }
    return $base_url . $this
      ->getPrefix() . $this
      ->get($name);
  }

  /**
   * Map the API block to CloudBlock properties.
   *
   * @param object $block
   *   Cloud Block from API.
   */
  protected function mapFromBlock(\stdClass $block) {
    $structure = [
      'name' => $block->name,
      'version' => $block->version,
      'label' => $block->config->name,
      'description' => $block->package->description,
      'js' => $block->config->js ?? NULL,
      'screenshot' => $block->config->screenshot ?? NULL,
      'edit_css' => $block->config->editor ?? NULL,
      'view_css' => $block->config->css ?? NULL,
      'raw' => $block,
    ];
    $this
      ->setConfig($structure);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CloudBlock::$raw protected property Raw block retrieved from API.
CloudBlock::getAssetUrl public function Get asset URL.
CloudBlock::getPrefix public function Defines prefix for accessing the assets.
CloudBlock::getRaw public function Getter for raw block.
CloudBlock::mapFromBlock protected function Map the API block to CloudBlock properties.
CloudBlock::__construct public function CloudBlock constructor. Overrides CloudBlockBase::__construct
CloudBlockBase::$description protected property Block description.
CloudBlockBase::$edit_css protected property Block edit css.
CloudBlockBase::$js protected property Block js.
CloudBlockBase::$label protected property Block label (human readable name).
CloudBlockBase::$name protected property Block machine name.
CloudBlockBase::$screenshot protected property Block screenshot.
CloudBlockBase::$version protected property Block version.
CloudBlockBase::$view_css protected property Block view css.
CloudBlockBase::get public function Getter for object properties. Overrides CloudBlockInterface::get
CloudBlockBase::getConfig public function Getter for properties related to Cloud Block config. Overrides CloudBlockInterface::getConfig
CloudBlockBase::getDescription public function Getter for description. Overrides CloudBlockInterface::getDescription
CloudBlockBase::getEditCss public function Getter for edit css. Overrides CloudBlockInterface::getEditCss
CloudBlockBase::getJs public function Getter for js. Overrides CloudBlockInterface::getJs
CloudBlockBase::getLabel public function Getter for label. Overrides CloudBlockInterface::getLabel
CloudBlockBase::getName public function Getter for name. Overrides CloudBlockInterface::getName
CloudBlockBase::getScreenshot public function Getter for screenshot. Overrides CloudBlockInterface::getScreenshot
CloudBlockBase::getVersion public function Getter for version. Overrides CloudBlockInterface::getVersion
CloudBlockBase::getViewCss public function Getter for view css. Overrides CloudBlockInterface::getViewCss
CloudBlockBase::setConfig protected function Sets object properties based on a given array.