class ContactFormViewBuilder in Contact Storage 8
Provides a contact form view builder.
Hierarchy
- class \Drupal\contact_storage\ContactFormViewBuilder implements EntityHandlerInterface, EntityViewBuilderInterface
Expanded class hierarchy of ContactFormViewBuilder
See also
\Drupal\contact\Entity\ContactForm
File
- src/
ContactFormViewBuilder.php, line 22
Namespace
Drupal\contact_storageView source
class ContactFormViewBuilder implements EntityViewBuilderInterface, EntityHandlerInterface {
/**
* The entity form builder.
*
* @var \Drupal\Core\Entity\EntityFormBuilderInterface
*/
protected $entityFormBuilder;
/**
* The renderer.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/**
* The contact settings config object.
*
* @var \Drupal\Core\Config\Config
*/
protected $config;
/**
* The contact message storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $contactMessageStorage;
/**
* Constructs a new contact form view builder.
*
* @param \Drupal\Core\Entity\EntityFormBuilderInterface $entity_form_builder
* The entity form builder service.
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer service.
* @param \Drupal\Core\Config\Config $config
* The contact settings config object.
* @param \Drupal\Core\Entity\EntityStorageInterface $contact_message_storage
* The contact message storage.
*/
public function __construct(EntityFormBuilderInterface $entity_form_builder, RendererInterface $renderer, Config $config, EntityStorageInterface $contact_message_storage) {
$this->entityFormBuilder = $entity_form_builder;
$this->renderer = $renderer;
$this->config = $config;
$this->contactMessageStorage = $contact_message_storage;
}
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($container
->get('entity.form_builder'), $container
->get('renderer'), $container
->get('config.factory')
->get('contact.settings'), $container
->get('entity_type.manager')
->getStorage('contact_message'));
}
/**
* {@inheritdoc}
*/
public function view(EntityInterface $entity, $view_mode = 'full', $langcode = NULL) {
if ($entity
->status()) {
$message = $this->contactMessageStorage
->create([
'contact_form' => $entity
->id(),
]);
$form = $this->entityFormBuilder
->getForm($message);
$form['#title'] = $entity
->label();
$form['#cache']['contexts'][] = 'user.permissions';
$this->renderer
->addCacheableDependency($form, $this->config);
}
else {
// Form disabled, display a custom message using a template.
$form['disabled_form_error'] = [
'#theme' => 'contact_storage_disabled_form',
'#contact_form' => $entity,
'#redirect_uri' => $entity
->getThirdPartySetting('contact_storage', 'redirect_uri', ''),
'#disabled_form_message' => $entity
->getThirdPartySetting('contact_storage', 'disabled_form_message', t('This contact form has been disabled.')),
];
}
// Add required cacheability metadata from the contact form entity, so that
// changing it invalidates the cache.
$this->renderer
->addCacheableDependency($form, $entity);
return $form;
}
/**
* {@inheritdoc}
*/
public function viewMultiple(array $entities = [], $view_mode = 'full', $langcode = NULL) {
$build = [];
foreach ($entities as $key => $entity) {
$build[$key] = $this
->view($entity, $view_mode, $langcode);
}
return $build;
}
/**
* {@inheritdoc}
*/
public function resetCache(array $entities = NULL) {
// Intentionally empty.
}
/**
* {@inheritdoc}
*/
public function getCacheTags() {
// Intentionally empty.
}
/**
* {@inheritdoc}
*/
public function buildComponents(array &$build, array $entities, array $displays, $view_mode) {
throw new \LogicException();
}
/**
* {@inheritdoc}
*/
public function viewField(FieldItemListInterface $items, $display_options = []) {
throw new \LogicException();
}
/**
* {@inheritdoc}
*/
public function viewFieldItem(FieldItemInterface $item, $display_options = []) {
throw new \LogicException();
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ContactFormViewBuilder:: |
protected | property | The contact settings config object. | |
ContactFormViewBuilder:: |
protected | property | The contact message storage. | |
ContactFormViewBuilder:: |
protected | property | The entity form builder. | |
ContactFormViewBuilder:: |
protected | property | The renderer. | |
ContactFormViewBuilder:: |
public | function |
Builds the component fields and properties of a set of entities. Overrides EntityViewBuilderInterface:: |
|
ContactFormViewBuilder:: |
public static | function |
Instantiates a new instance of this entity handler. Overrides EntityHandlerInterface:: |
|
ContactFormViewBuilder:: |
public | function |
The cache tag associated with this entity view builder. Overrides EntityViewBuilderInterface:: |
|
ContactFormViewBuilder:: |
public | function |
Resets the entity render cache. Overrides EntityViewBuilderInterface:: |
|
ContactFormViewBuilder:: |
public | function |
Builds the render array for the provided entity. Overrides EntityViewBuilderInterface:: |
|
ContactFormViewBuilder:: |
public | function |
Builds a renderable array for the value of a single field in an entity. Overrides EntityViewBuilderInterface:: |
|
ContactFormViewBuilder:: |
public | function |
Builds a renderable array for a single field item. Overrides EntityViewBuilderInterface:: |
|
ContactFormViewBuilder:: |
public | function |
Builds the render array for the provided entities. Overrides EntityViewBuilderInterface:: |
|
ContactFormViewBuilder:: |
public | function | Constructs a new contact form view builder. |