class MailchimpCampaignViewBuilder in Mailchimp 8
Same name and namespace in other branches
- 2.x modules/mailchimp_campaign/src/Entity/MailchimpCampaignViewBuilder.php \Drupal\mailchimp_campaign\Entity\MailchimpCampaignViewBuilder
Defines the render controller for MailchimpCampaign entities.
Hierarchy
- class \Drupal\Core\Entity\EntityHandlerBase uses DependencySerializationTrait, StringTranslationTrait
- class \Drupal\Core\Entity\EntityViewBuilder implements EntityHandlerInterface, EntityViewBuilderInterface, TrustedCallbackInterface uses DeprecatedServicePropertyTrait
- class \Drupal\mailchimp_campaign\Entity\MailchimpCampaignViewBuilder
- class \Drupal\Core\Entity\EntityViewBuilder implements EntityHandlerInterface, EntityViewBuilderInterface, TrustedCallbackInterface uses DeprecatedServicePropertyTrait
Expanded class hierarchy of MailchimpCampaignViewBuilder
File
- modules/
mailchimp_campaign/ src/ Entity/ MailchimpCampaignViewBuilder.php, line 21
Namespace
Drupal\mailchimp_campaign\EntityView source
class MailchimpCampaignViewBuilder extends EntityViewBuilder {
/**
* The date formatter service.
*
* @var \Drupal\Core\Datetime\DateFormatterInterface
*/
protected $dateFormatter;
/**
* The messenger service.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected $messenger;
/**
* Constructs a new MailchimpCampaignViewBuilder.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type definition.
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager service.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
* @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
* The date formatter service.
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* The messenger service.
*/
public function __construct(EntityTypeInterface $entity_type, EntityManagerInterface $entity_manager, LanguageManagerInterface $language_manager, DateFormatterInterface $date_formatter, MessengerInterface $messenger) {
parent::__construct($entity_type, $entity_manager, $language_manager);
$this->dateFormatter = $date_formatter;
$this->messenger = $messenger;
}
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->get('entity.manager'), $container
->get('language_manager'), $container
->get('date.formatter'), $container
->get('messenger'));
}
/**
* {@inheritdoc}
*/
public function view(EntityInterface $entity, $view_mode = 'full', $langcode = NULL) {
$build = parent::view($entity, $view_mode, $langcode);
// Attach campaign JS and CSS.
$build['#attached']['library'][] = 'mailchimp_campaign/campaign-view';
// Prepare rendered content.
/* @var $entity \Drupal\mailchimp_campaign\Entity\MailchimpCampaign */
$content = $this
->renderTemplate($entity
->getTemplate());
$rendered = '';
foreach ($content as $key => $section) {
$rendered .= "<h3>{$key}</h3>" . $section;
}
if (!$entity
->isInitialized()) {
$this->messenger
->addError($this
->t('The campaign could not be found.'));
return $build;
}
// Get the template name.
$mc_template = mailchimp_campaign_get_template($entity->mc_data->settings->template_id);
$mc_template_name = isset($mc_template) ? $mc_template->name : '';
$list_segment_name = 'N/A';
$list_segments = mailchimp_campaign_get_list_segments($entity->list->id, 'saved');
if (isset($entity->mc_data->recipients->segment_opts->saved_segment_id)) {
foreach ($list_segments as $list_segment) {
if ($list_segment->id == $entity->mc_data->recipients->segment_opts->saved_segment_id) {
$list_segment_name = $list_segment->name;
}
}
}
$list_url = Url::fromUri('https://admin.mailchimp.com/lists/dashboard/overview?id=' . $entity->list->id, [
'attributes' => [
'target' => '_blank',
],
]);
$archive_url = Url::fromUri($entity->mc_data->archive_url, [
'attributes' => [
'target' => '_blank',
],
]);
$send_time = 'N/A';
if (isset($entity->mc_data->send_time) && $entity->mc_data->send_time) {
$send_time = $this->dateFormatter
->format(strtotime($entity->mc_data->send_time), 'custom', 'F j, Y - g:ia');
}
$fields = [
'title' => [
'label' => $this
->t('Title'),
'value' => $entity->mc_data->settings->title,
],
'subject' => [
'label' => $this
->t('Subject'),
'value' => $entity->mc_data->settings->subject_line,
],
'list' => [
'label' => $this
->t('Mailchimp Audience'),
'value' => Link::fromTextAndUrl($entity->list->name, $list_url)
->toString(),
],
'list_segment' => [
'label' => $this
->t('Audience Tags'),
'value' => $list_segment_name,
],
'from_email' => [
'label' => $this
->t('From Email'),
'value' => $entity->mc_data->settings->reply_to,
],
'from_name' => [
'label' => $this
->t('From Name'),
'value' => $entity->mc_data->settings->from_name,
],
'template' => [
'label' => $this
->t('Template'),
'value' => $mc_template_name,
],
'type' => [
'label' => $this
->t('Audience type'),
'value' => $entity->mc_data->type,
],
'status' => [
'label' => $this
->t('Status'),
'value' => $entity->mc_data->status,
],
'emails_sent' => [
'label' => $this
->t('Emails sent'),
'value' => $entity->mc_data->emails_sent,
],
'send_time' => [
'label' => $this
->t('Send time'),
'value' => $send_time,
],
'content' => [
'label' => $this
->t('Rendered template HTML (@archive)', [
'@archive' => Link::fromTextAndUrl('View Mailchimp archive', $archive_url)
->toString(),
]),
'value' => $rendered,
],
];
foreach ($fields as $key => $field) {
$build[$key] = [
'#prefix' => "<div class=\"field campaign-{$key}\"><h3 class=\"field-label\">{$field['label']}</h3>",
'#markup' => "<p>{$field['value']}</p>",
'#suffix' => '</div>',
];
}
return $build;
}
/**
* Converts a template into rendered content.
*
* @param array $template
* Array of template sections.
*
* @return array
* Array of template content indexed by section ID.
*/
private function renderTemplate(array $template) {
$content = [];
foreach ($template as $key => $part) {
if (isset($part['format'])) {
$content[$key] = check_markup($part['value'], $part['format']);
}
}
return $content;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
DeprecatedServicePropertyTrait:: |
public | function | Allows to access deprecated/removed properties. | |
EntityHandlerBase:: |
protected | property | The module handler to invoke hooks on. | 2 |
EntityHandlerBase:: |
protected | function | Gets the module handler. | 2 |
EntityHandlerBase:: |
public | function | Sets the module handler for this handler. | |
EntityViewBuilder:: |
protected | property | The cache bin used to store the render cache. | |
EntityViewBuilder:: |
protected | property | ||
EntityViewBuilder:: |
protected | property | The entity display repository. | |
EntityViewBuilder:: |
protected | property | The entity repository service. | |
EntityViewBuilder:: |
protected | property | Information about the entity type. | |
EntityViewBuilder:: |
protected | property | The type of entities for which this view builder is instantiated. | |
EntityViewBuilder:: |
protected | property | The language manager. | |
EntityViewBuilder:: |
protected | property | The EntityViewDisplay objects created for individual field rendering. | |
EntityViewBuilder:: |
protected | property | The theme registry. | |
EntityViewBuilder:: |
protected | function | Add contextual links. | |
EntityViewBuilder:: |
protected | function | Specific per-entity building. | 1 |
EntityViewBuilder:: |
public | function | Builds an entity's view; augments entity defaults. | |
EntityViewBuilder:: |
public | function |
Builds the component fields and properties of a set of entities. Overrides EntityViewBuilderInterface:: |
6 |
EntityViewBuilder:: |
public | function | Builds multiple entities' views; augments entity defaults. | |
EntityViewBuilder:: |
protected | function | Provides entity-specific defaults to the build process. | 4 |
EntityViewBuilder:: |
public | function |
The cache tag associated with this entity view builder. Overrides EntityViewBuilderInterface:: |
|
EntityViewBuilder:: |
protected | function | Gets an EntityViewDisplay for rendering an individual field. | |
EntityViewBuilder:: |
protected | function | Determines whether the view mode is cacheable. | |
EntityViewBuilder:: |
public | function |
Resets the entity render cache. Overrides EntityViewBuilderInterface:: |
|
EntityViewBuilder:: |
public static | function |
Lists the trusted callbacks provided by the implementing class. Overrides TrustedCallbackInterface:: |
2 |
EntityViewBuilder:: |
public | function |
Builds a renderable array for the value of a single field in an entity. Overrides EntityViewBuilderInterface:: |
|
EntityViewBuilder:: |
public | function |
Builds a renderable array for a single field item. Overrides EntityViewBuilderInterface:: |
|
EntityViewBuilder:: |
public | function |
Builds the render array for the provided entities. Overrides EntityViewBuilderInterface:: |
4 |
MailchimpCampaignViewBuilder:: |
protected | property | The date formatter service. | |
MailchimpCampaignViewBuilder:: |
protected | property | The messenger service. | |
MailchimpCampaignViewBuilder:: |
public static | function |
Instantiates a new instance of this entity handler. Overrides EntityViewBuilder:: |
|
MailchimpCampaignViewBuilder:: |
private | function | Converts a template into rendered content. | |
MailchimpCampaignViewBuilder:: |
public | function |
Builds the render array for the provided entity. Overrides EntityViewBuilder:: |
|
MailchimpCampaignViewBuilder:: |
public | function |
Constructs a new MailchimpCampaignViewBuilder. Overrides EntityViewBuilder:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
TrustedCallbackInterface:: |
constant | Untrusted callbacks throw exceptions. | ||
TrustedCallbackInterface:: |
constant | Untrusted callbacks trigger silenced E_USER_DEPRECATION errors. | ||
TrustedCallbackInterface:: |
constant | Untrusted callbacks trigger E_USER_WARNING errors. |