Skip to content

Instantly share code, notes, and snippets.

@EclipseGc
Last active December 7, 2016 19:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EclipseGc/f17034c1eb45badc6c824923d93fa856 to your computer and use it in GitHub Desktop.
Save EclipseGc/f17034c1eb45badc6c824923d93fa856 to your computer and use it in GitHub Desktop.
<?php
namespace Drupal\lightning_layout;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
class PanelizerAwareEntityViewDisplay extends EntityViewDisplay {
protected function ensurePanelizerFieldStorage() {
$storage = $this->entityTypeManager()->getStorage('field_storage_config');
$entity = $storage->load(
$this->getTargetEntityTypeId() . '.panelizer'
);
if (empty($entity)) {
$entity = $storage->create([
'entity_type' => $this->getTargetEntityTypeId(),
'field_name' => 'panelizer',
'type' => 'panelizer',
'cardinality' => -1,
'settings' => [],
'status' => TRUE,
]);
$entity->save();
}
return $entity;
}
protected function ensurePanelizerField() {
$storage = $this->entityTypeManager()->getStorage('field_config');
$entity = $storage->load(
$this->getTargetEntityTypeId() . '.' . $this->getTargetBundle() . '.panelizer'
);
if (empty($entity)) {
$entity = $storage->create([
'field_storage' => $this->ensurePanelizerFieldStorage(),
'bundle' => $this->getTargetBundle(),
'label' => t('Panelizer'),
'settings' => [],
]);
$entity->save();
}
return $entity;
}
public function isPanelized() {
return (bool) $this->getThirdPartySetting('panelizer', 'enable', FALSE);
}
public function setPanelized($status = TRUE) {
$status = (bool) $status;
if ($status) {
$this->ensurePanelizerField();
}
return $this->setThirdPartySetting('panelizer', 'enable', $status);
}
public function isCustomizable() {
return (bool) $this->getThirdPartySetting('panelizer', 'custom', FALSE);
}
public function setCustomizable($status = TRUE) {
return $this->setThirdPartySetting('panelizer', 'custom', (bool) $status);
}
public function isChooseable() {
return (bool) $this->getThirdPartySetting('panelizer', 'allow', FALSE);
}
public function setChooseable($status = TRUE) {
return $this->setThirdPartySetting('panelizer', 'allow', (bool) $status);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment