Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / chi-teck / drupal-code-generator / templates / d8 / service / custom.twig
diff --git a/vendor/chi-teck/drupal-code-generator/templates/d8/service/custom.twig b/vendor/chi-teck/drupal-code-generator/templates/d8/service/custom.twig
new file mode 100644 (file)
index 0000000..1f7bf52
--- /dev/null
@@ -0,0 +1,41 @@
+<?php
+
+namespace Drupal\{{ machine_name }};
+
+use Drupal\Core\Entity\EntityTypeManagerInterface;
+
+/**
+ * {{ class }} service.
+ */
+class {{ class }} {
+
+  /**
+   * Node storage.
+   *
+   * @var \Drupal\Node\NodeStorageInterface
+   */
+  protected $nodeStorage;
+
+  /**
+   * Constructs {{ class|article }} object.
+   *
+   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
+   *   The entity type manager.
+   */
+  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
+    $this->nodeStorage = $entity_type_manager->getStorage('node');
+  }
+
+  /**
+   * Retrieves the last created node.
+   */
+  public function getLastNode() {
+    $nids = $this->nodeStorage->getQuery()
+      ->sort('created', 'DESC')
+      ->range(0, 1)
+      ->execute();
+    $nid = reset($nids);
+    return $nid ? $this->nodeStorage->load($nid) : FALSE;
+  }
+
+}