You are here

Webhook.php in Webhooks 8

File

modules/webhook/src/Entity/Webhook.php
View source
<?php

namespace Drupal\webhook\Entity;

use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\webhook\WebhookInterface;

/**
 * Defines the webhook entity class.
 *
 * @ContentEntityType(
 *   id = "webhook",
 *   label = @Translation("Webhook"),
 *   label_collection = @Translation("Webhooks"),
 *   handlers = {
 *     "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
 *     "list_builder" = "Drupal\webhook\WebhookListBuilder",
 *     "views_data" = "Drupal\views\EntityViewsData",
 *     "form" = {
 *       "add" = "Drupal\webhook\Form\WebhookForm",
 *       "edit" = "Drupal\webhook\Form\WebhookForm",
 *       "delete" = "Drupal\Core\Entity\ContentEntityDeleteForm"
 *     },
 *     "route_provider" = {
 *       "html" = "Drupal\Core\Entity\Routing\AdminHtmlRouteProvider",
 *     }
 *   },
 *   base_table = "webhook",
 *   admin_permission = "access webhook overview",
 *   entity_keys = {
 *     "id" = "id",
 *     "label" = "title",
 *     "uuid" = "uuid"
 *   },
 *   links = {
 *     "add-form" = "/admin/content/webhook/add",
 *     "canonical" = "/webhook/{webhook}",
 *     "edit-form" = "/admin/content/webhook/{webhook}/edit",
 *     "delete-form" = "/admin/content/webhook/{webhook}/delete",
 *     "collection" = "/admin/content/webhook"
 *   },
 * )
 */
class Webhook extends ContentEntityBase implements WebhookInterface {

  /**
   * {@inheritdoc}
   */
  public function getTitle() {
    return $this
      ->get('title')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function setTitle($title) {
    $this
      ->set('title', $title);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getCreatedTime() {
    return $this
      ->get('created')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function setCreatedTime($timestamp) {
    $this
      ->set('created', $timestamp);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
    $fields = parent::baseFieldDefinitions($entity_type);
    $fields['title'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Title'))
      ->setDescription(t('The title of the webhook entity.'))
      ->setRequired(TRUE)
      ->setSetting('max_length', 255)
      ->setDisplayOptions('form', [
      'type' => 'string_textfield',
      'weight' => -5,
    ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayOptions('view', [
      'label' => 'hidden',
      'type' => 'string',
      'weight' => -5,
    ])
      ->setDisplayConfigurable('view', TRUE);
    $fields['headers'] = BaseFieldDefinition::create('string_long')
      ->setLabel(t('Headers'))
      ->setDescription(t('The headers of the webhook entity.'))
      ->setRequired(FALSE)
      ->setDisplayOptions('form', [
      'type' => 'string_textarea',
      'weight' => -5,
      'settings' => [
        'rows' => 12,
      ],
    ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayOptions('view', [
      'label' => 'hidden',
      'type' => 'string',
      'weight' => -5,
    ])
      ->setDisplayConfigurable('view', TRUE);
    $fields['payload'] = BaseFieldDefinition::create('string_long')
      ->setLabel(t('Payload'))
      ->setDescription(t('The payload of the webhook entity.'))
      ->setRequired(FALSE)
      ->setDisplayOptions('form', [
      'type' => 'string_textarea',
      'weight' => -5,
      'settings' => [
        'rows' => 12,
      ],
    ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayOptions('view', [
      'label' => 'hidden',
      'type' => 'string',
      'weight' => -5,
    ])
      ->setDisplayConfigurable('view', TRUE);
    $fields['created'] = BaseFieldDefinition::create('created')
      ->setLabel(t('Authored on'))
      ->setDescription(t('The time that the webhook was created.'))
      ->setDisplayOptions('view', [
      'label' => 'above',
      'type' => 'timestamp',
      'weight' => 20,
    ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayOptions('form', [
      'type' => 'datetime_timestamp',
      'weight' => 20,
    ])
      ->setDisplayConfigurable('view', TRUE);
    return $fields;
  }

}

Classes

Namesort descending Description
Webhook Defines the webhook entity class.