NodeType.php 5.85 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\node\Entity\NodeType.
6 7
 */

8
namespace Drupal\node\Entity;
9

10
use Drupal\Component\Utility\NestedArray;
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Entity\EntityStorageControllerInterface;
use Drupal\node\NodeTypeInterface;

/**
 * Defines the Node type configuration entity.
 *
 * @EntityType(
 *   id = "node_type",
 *   label = @Translation("Content type"),
 *   controllers = {
 *     "storage" = "Drupal\Core\Config\Entity\ConfigStorageController",
 *     "access" = "Drupal\node\NodeTypeAccessController",
 *     "form" = {
 *       "add" = "Drupal\node\NodeTypeFormController",
 *       "edit" = "Drupal\node\NodeTypeFormController",
 *       "delete" = "Drupal\node\Form\NodeTypeDeleteConfirm"
 *     },
 *     "list" = "Drupal\node\NodeTypeListController",
 *   },
31
 *   admin_permission = "administer content types",
32
 *   config_prefix = "node.type",
33
 *   bundle_of = "node",
34 35 36 37
 *   entity_keys = {
 *     "id" = "type",
 *     "label" = "name",
 *     "uuid" = "uuid"
38 39
 *   },
 *   links = {
40
 *     "edit-form" = "node.type_edit"
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
 *   }
 * )
 */
class NodeType extends ConfigEntityBase implements NodeTypeInterface {

  /**
   * The machine name of this node type.
   *
   * @var string
   *
   * @todo Rename to $id.
   */
  public $type;

  /**
   * The UUID of the node type.
   *
   * @var string
   */
  public $uuid;

  /**
   * The human-readable name of the node type.
   *
   * @var string
   *
   * @todo Rename to $label.
   */
  public $name;

  /**
   * A brief description of this node type.
   *
   * @var string
   */
  public $description;

  /**
   * Help information shown to the user when creating a Node of this type.
   *
   * @var string
   */
  public $help;

  /**
   * Indicates whether the Node entity of this type has a title.
   *
   * @var bool
   *
   * @todo Rename to $node_has_title.
   */
  public $has_title = TRUE;

  /**
   * The label to use for the title of a Node of this type in the user interface.
   *
   * @var string
   *
   * @todo Rename to $node_title_label.
   */
  public $title_label = 'Title';

  /**
   * Indicates whether a Body field should be created for this node type.
   *
   * This property affects entity creation only. It allows default configuration
   * of modules and installation profiles to specify whether a Body field should
   * be created for this bundle.
   *
   * @var bool
   *
112
   * @see \Drupal\node\Entity\NodeType::$create_body_label
113 114 115 116 117 118
   */
  protected $create_body = TRUE;

  /**
   * The label to use for the Body field upon entity creation.
   *
119
   * @see \Drupal\node\Entity\NodeType::$create_body
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
   *
   * @var string
   */
  protected $create_body_label = 'Body';

  /**
   * Module-specific settings for this node type, keyed by module name.
   *
   * @var array
   *
   * @todo Pluginify.
   */
  public $settings = array();

  /**
   * {@inheritdoc}
   */
  public function id() {
    return $this->type;
  }

  /**
   * {@inheritdoc}
   */
  public function getModuleSettings($module) {
    if (isset($this->settings[$module]) && is_array($this->settings[$module])) {
      return $this->settings[$module];
    }
    return array();
  }

  /**
   * {@inheritdoc}
   */
  public function isLocked() {
    $locked = \Drupal::state()->get('node.type.locked');
    return isset($locked[$this->id()]) ? $locked[$this->id()] : FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function postSave(EntityStorageControllerInterface $storage_controller, $update = TRUE) {
163 164
    parent::postSave($storage_controller, $update);

165 166 167 168 169 170
    if (!$update) {
      // Clear the node type cache, so the new type appears.
      \Drupal::cache()->deleteTags(array('node_types' => TRUE));

      entity_invoke_bundle_hook('create', 'node', $this->id());

171 172 173
      // Create a body if the create_body property is true and we're not in
      // the syncing process.
      if ($this->get('create_body') && !$this->isSyncing()) {
174 175 176 177
        $label = $this->get('create_body_label');
        node_add_body_field($this, $label);
      }
    }
178
    elseif ($this->getOriginalId() != $this->id()) {
179 180 181
      // Clear the node type cache to reflect the rename.
      \Drupal::cache()->deleteTags(array('node_types' => TRUE));

182
      $update_count = node_type_update_nodes($this->getOriginalId(), $this->id());
183 184 185 186 187
      if ($update_count) {
        drupal_set_message(format_plural($update_count,
          'Changed the content type of 1 post from %old-type to %type.',
          'Changed the content type of @count posts from %old-type to %type.',
          array(
188
            '%old-type' => $this->getOriginalId(),
189 190 191
            '%type' => $this->id(),
          )));
      }
192
      entity_invoke_bundle_hook('rename', 'node', $this->getOriginalId(), $this->id());
193 194 195 196 197 198 199 200 201 202 203
    }
    else {
      // Invalidate the cache tag of the updated node type only.
      cache()->invalidateTags(array('node_type' => $this->id()));
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function postDelete(EntityStorageControllerInterface $storage_controller, array $entities) {
204 205
    parent::postDelete($storage_controller, $entities);

206 207 208 209 210 211 212
    // Clear the node type cache to reflect the removal.
    $storage_controller->resetCache(array_keys($entities));
    foreach ($entities as $entity) {
      entity_invoke_bundle_hook('delete', 'node', $entity->id());
    }
  }

213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
  /**
   * {@inheritdoc}
   */
  public static function preCreate(EntityStorageControllerInterface $storage_controller, array &$values) {
    parent::preCreate($storage_controller, $values);

    // Ensure default values are set.
    if (!isset($values['settings']['node'])) {
      $values['settings']['node'] = array();
    }
    $values['settings']['node'] = NestedArray::mergeDeep(array(
      'options' => array(
        'status' => TRUE,
        'promote' => TRUE,
        'sticky' => FALSE,
        'revision' => FALSE,
      ),
      'preview' => DRUPAL_OPTIONAL,
      'submitted' => TRUE,
    ), $values['settings']['node']);
  }

235
}