FormEntityContext.php in Flexiform 8
File
src/FormEntity/FormEntityContext.php
View source
<?php
namespace Drupal\flexiform\FormEntity;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Plugin\Context\Context;
use Drupal\Core\Plugin\Context\ContextDefinition;
class FormEntityContext extends Context implements FormEntityContextInterface {
use DependencySerializationTrait;
protected $entityNamespace;
protected $formEntity;
public function setEntityNamespace($namespace) {
$this->entityNamespace = $namespace;
}
public function getEntityNamespace() {
return $this->entityNamespace;
}
public function getFormEntity() {
return $this->formEntity;
}
public function setFormEntity(FlexiformFormEntityInterface $form_entity) {
$this->formEntity = $form_entity;
}
public function hasContextValue() {
if (!$this->contextData) {
$this
->getContextValue();
}
return parent::hasContextValue();
}
public function getContextValue() {
if (!$this->contextData) {
$this
->setContextValue($this
->getFormEntity()
->getEntity());
}
return parent::getContextValue();
}
public static function createFromFlexiformFormEntity(FlexiformFormEntityInterface $form_entity, FieldableEntityInterface $entity = NULL) {
$context_definition = new ContextDefinition('entity:' . $form_entity
->getEntityType(), $form_entity
->getLabel());
$context_definition
->addConstraint('Bundle', [
$form_entity
->getBundle(),
]);
$context = new static($context_definition, $entity);
$context
->setFormEntity($form_entity);
return $context;
}
}