Skip to content

Instantly share code, notes, and snippets.

@georgbez
Last active August 7, 2017 08:06
Show Gist options
  • Save georgbez/55ed45ad128c4993e255486577199025 to your computer and use it in GitHub Desktop.
Save georgbez/55ed45ad128c4993e255486577199025 to your computer and use it in GitHub Desktop.
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..e6e2d16
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+.idea/
+/SchemaRecipe.patch
\ No newline at end of file
diff --git a/makePatch.txt b/makePatch.txt
new file mode 100644
index 0000000..a02f3cf
--- /dev/null
+++ b/makePatch.txt
@@ -0,0 +1 @@
+git diff 8.x-1.x..feature/SchemaRecipe > SchemaRecipe.patch
\ No newline at end of file
diff --git a/schema_recipe/schema_recipe.info.yml b/schema_recipe/schema_recipe.info.yml
new file mode 100644
index 0000000..97e44db
--- /dev/null
+++ b/schema_recipe/schema_recipe.info.yml
@@ -0,0 +1,7 @@
+name: Schema.org Recipe
+type: module
+description: Adds Schema.org/Recipe to the JSON LD array.
+core: 8.x
+package: SEO
+dependencies:
+ - schema_metatag
diff --git a/schema_recipe/schema_recipe.module b/schema_recipe/schema_recipe.module
new file mode 100644
index 0000000..16f7735
--- /dev/null
+++ b/schema_recipe/schema_recipe.module
@@ -0,0 +1,21 @@
+<?php
+
+/**
+ * @file
+ * Contains schema_recipe.module.
+ */
+
+/**
+ * Implements hook_metatag_groups_alter().
+ *
+ * Every module should implement this to add a flag to the object types they
+ * create.
+ */
+function schema_recipe_metatag_groups_alter(&$data) {
+ $module_name = basename(__FILE__, '.module');
+ foreach ($data as $key => $value) {
+ if ($value['provider'] == $module_name) {
+ $data[$key]['schema_metatag'] = TRUE;
+ }
+ }
+}
diff --git a/schema_recipe/src/Plugin/metatag/Group/SchemaRecipe.php b/schema_recipe/src/Plugin/metatag/Group/SchemaRecipe.php
new file mode 100644
index 0000000..d115787
--- /dev/null
+++ b/schema_recipe/src/Plugin/metatag/Group/SchemaRecipe.php
@@ -0,0 +1,19 @@
+<?php
+
+namespace Drupal\schema_recipe\Plugin\metatag\Group;
+
+use \Drupal\schema_metatag\Plugin\metatag\Group\SchemaGroupBase;
+
+/**
+ * Provides a plugin for the 'Recipe' meta tag group.
+ *
+ * @MetatagGroup(
+ * id = "schema_recipe",
+ * label = @Translation("Schema.org: Recipe"),
+ * description = @Translation("See Schema.org definitions for this Schema type at <a href="":url"">:url</a>.", arguments = { ":url" = "http://schema.org/Recipe"}),
+ * weight = 10,
+ * )
+ */
+class SchemaRecipe extends SchemaGroupBase {
+ // Nothing here yet. Just a placeholder class for a plugin.
+}
diff --git a/schema_recipe/src/Plugin/metatag/Tag/SchemaRecipeAuthor.php b/schema_recipe/src/Plugin/metatag/Tag/SchemaRecipeAuthor.php
new file mode 100644
index 0000000..b0b54d9
--- /dev/null
+++ b/schema_recipe/src/Plugin/metatag/Tag/SchemaRecipeAuthor.php
@@ -0,0 +1,41 @@
+<?php
+
+namespace Drupal\schema_recipe\Plugin\metatag\Tag;
+
+use \Drupal\schema_metatag\Plugin\metatag\Tag\SchemaPersonOrgBase;
+
+/**
+ * Provides a plugin for the 'author' meta tag.
+ *
+ * - 'id' should be a globally unique id.
+ * - 'name' should match the Schema.org element name.
+ * - 'group' should match the id of the group that defines the Schema.org type.
+ *
+ * @MetatagTag(
+ * id = "schema_recipe_author",
+ * label = @Translation("author"),
+ * description = @Translation("Author of the recipe."),
+ * name = "author",
+ * group = "schema_recipe",
+ * weight = 5,
+ * type = "string",
+ * secure = FALSE,
+ * multiple = TRUE
+ * )
+ */
+class SchemaRecipeAuthor extends SchemaPersonOrgBase {
+
+ /**
+ * Generate a form element for this meta tag.
+ *
+ * We need multiple values, so create a tree of values and
+ * stored the serialized value as a string.
+ */
+ public function form(array $element = []) {
+ $form = parent::form($element);
+ $form['name']['#attribute']['placeholder'] = '[node:author:display-name]';
+ $form['url']['#attributes']['placeholder'] = '[node:author:url]';
+ return $form;
+ }
+
+}
diff --git a/schema_recipe/src/Plugin/metatag/Tag/SchemaRecipeCookTime.php b/schema_recipe/src/Plugin/metatag/Tag/SchemaRecipeCookTime.php
new file mode 100644
index 0000000..d673a02
--- /dev/null
+++ b/schema_recipe/src/Plugin/metatag/Tag/SchemaRecipeCookTime.php
@@ -0,0 +1,40 @@
+<?php
+
+namespace Drupal\schema_recipe\Plugin\metatag\Tag;
+
+use \Drupal\schema_metatag\Plugin\metatag\Tag\SchemaDateBase;
+
+/**
+ * Provides a plugin for the 'cookTime' meta tag.
+ *
+ * - 'id' should be a globally unique id.
+ * - 'name' should match the Schema.org element name.
+ * - 'group' should match the id of the group that defines the Schema.org type.
+ *
+ * @MetatagTag(
+ * id = "schema_recipe_cook_time",
+ * label = @Translation("cookTime"),
+ * description = @Translation("Cook time for recipe."),
+ * name = "cookTime",
+ * group = "schema_recipe",
+ * weight = 4,
+ * type = "string",
+ * secure = FALSE,
+ * multiple = FALSE
+ * )
+ */
+class SchemaRecipeCookTime extends SchemaDateBase {
+
+ /**
+ * Generate a form element for this meta tag.
+ *
+ * We need multiple values, so create a tree of values and
+ * stored the serialized value as a string.
+ */
+ public function form(array $element = []) {
+ $form = parent::form($element);
+ $form['#attributes']['placeholder'] = '[node:recipe_cook_time]';
+ return $form;
+ }
+
+}
diff --git a/schema_recipe/src/Plugin/metatag/Tag/SchemaRecipeDateModified.php b/schema_recipe/src/Plugin/metatag/Tag/SchemaRecipeDateModified.php
new file mode 100644
index 0000000..d2c9d40
--- /dev/null
+++ b/schema_recipe/src/Plugin/metatag/Tag/SchemaRecipeDateModified.php
@@ -0,0 +1,40 @@
+<?php
+
+namespace Drupal\schema_recipe\Plugin\metatag\Tag;
+
+use \Drupal\schema_metatag\Plugin\metatag\Tag\SchemaDateBase;
+
+/**
+ * Provides a plugin for the 'dateModified' meta tag.
+ *
+ * - 'id' should be a globally unique id.
+ * - 'name' should match the Schema.org element name.
+ * - 'group' should match the id of the group that defines the Schema.org type.
+ *
+ * @MetatagTag(
+ * id = "schema_recipe_date_modified",
+ * label = @Translation("dateModified"),
+ * description = @Translation("Date the recipe was last modified."),
+ * name = "dateModified",
+ * group = "schema_recipe",
+ * weight = 4,
+ * type = "string",
+ * secure = FALSE,
+ * multiple = FALSE
+ * )
+ */
+class SchemaRecipeDateModified extends SchemaDateBase {
+
+ /**
+ * Generate a form element for this meta tag.
+ *
+ * We need multiple values, so create a tree of values and
+ * stored the serialized value as a string.
+ */
+ public function form(array $element = []) {
+ $form = parent::form($element);
+ $form['#attributes']['placeholder'] = '[node:changed:html_datetime]';
+ return $form;
+ }
+
+}
diff --git a/schema_recipe/src/Plugin/metatag/Tag/SchemaRecipeDatePublished.php b/schema_recipe/src/Plugin/metatag/Tag/SchemaRecipeDatePublished.php
new file mode 100644
index 0000000..c5cd5e8
--- /dev/null
+++ b/schema_recipe/src/Plugin/metatag/Tag/SchemaRecipeDatePublished.php
@@ -0,0 +1,37 @@
+<?php
+
+namespace Drupal\schema_recipe\Plugin\metatag\Tag;
+
+use \Drupal\schema_metatag\Plugin\metatag\Tag\SchemaDateBase;
+
+/**
+ * Provides a plugin for the 'datePublished' meta tag.
+ *
+ * - 'id' should be a globally unique id.
+ * - 'name' should match the Schema.org element name.
+ * - 'group' should match the id of the group that defines the Schema.org type.
+ *
+ * @MetatagTag(
+ * id = "schema_recipe_date_published",
+ * label = @Translation("datePublished"),
+ * description = @Translation("Date the recipe was published."),
+ * name = "datePublished",
+ * group = "schema_recipe",
+ * weight = 3,
+ * type = "string",
+ * secure = FALSE,
+ * multiple = FALSE
+ * )
+ */
+class SchemaRecipeDatePublished extends SchemaDateBase {
+
+ /**
+ * Generate a form element for this meta tag.
+ */
+ public function form(array $element = []) {
+ $form = parent::form($element);
+ $form['#attributes']['placeholder'] = '[node:created:html_datetime]';
+ return $form;
+ }
+
+}
diff --git a/schema_recipe/src/Plugin/metatag/Tag/SchemaRecipeDescription.php b/schema_recipe/src/Plugin/metatag/Tag/SchemaRecipeDescription.php
new file mode 100644
index 0000000..8f27fe4
--- /dev/null
+++ b/schema_recipe/src/Plugin/metatag/Tag/SchemaRecipeDescription.php
@@ -0,0 +1,37 @@
+<?php
+
+namespace Drupal\schema_recipe\Plugin\metatag\Tag;
+
+use \Drupal\schema_metatag\Plugin\metatag\Tag\SchemaNameBase;
+
+/**
+ * Provides a plugin for the 'schema_recipe_description' meta tag.
+ *
+ * - 'id' should be a globally unique id.
+ * - 'name' should match the Schema.org element name.
+ * - 'group' should match the id of the group that defines the Schema.org type.
+ *
+ * @MetatagTag(
+ * id = "schema_recipe_description",
+ * label = @Translation("description"),
+ * description = @Translation("A description of the item."),
+ * name = "description",
+ * group = "schema_recipe",
+ * weight = 1,
+ * type = "string",
+ * secure = FALSE,
+ * multiple = FALSE
+ * )
+ */
+class SchemaRecipeDescription extends SchemaNameBase {
+
+ /**
+ * Generate a form element for this meta tag.
+ */
+ public function form(array $element = []) {
+ $form = parent::form($element);
+ $form['#attributes']['placeholder'] = '[node:summary]';
+ return $form;
+ }
+
+}
diff --git a/schema_recipe/src/Plugin/metatag/Tag/SchemaRecipeImage.php b/schema_recipe/src/Plugin/metatag/Tag/SchemaRecipeImage.php
new file mode 100644
index 0000000..820122d
--- /dev/null
+++ b/schema_recipe/src/Plugin/metatag/Tag/SchemaRecipeImage.php
@@ -0,0 +1,35 @@
+<?php
+
+namespace Drupal\schema_recipe\Plugin\metatag\Tag;
+
+use \Drupal\schema_metatag\Plugin\metatag\Tag\SchemaNameBase;
+
+/**
+ * Provides a plugin for the 'image' meta tag.
+ *
+ * - 'id' should be a globally unique id.
+ * - 'name' should match the Schema.org element name.
+ * - 'group' should match the id of the group that defines the Schema.org type.
+ *
+ * @MetatagTag(
+ * id = "schema_recipe_image",
+ * label = @Translation("image"),
+ * description = @Translation("The url of primary image for this item."),
+ * name = "image",
+ * group = "schema_recipe",
+ * weight = 0,
+ * type = "string",
+ * secure = FALSE,
+ * multiple = FALSE
+ * )
+ */
+class SchemaRecipeImage extends SchemaNameBase {
+ /**
+ * Generate a form element for this meta tag.
+ */
+ public function form(array $element = []) {
+ $form = parent::form($element);
+ $form['#attributes']['placeholder'] = '[node:field_teaser_media:entity:field_image:og_image]';
+ return $form;
+ }
+}
diff --git a/schema_recipe/src/Plugin/metatag/Tag/SchemaRecipeIngredient.php b/schema_recipe/src/Plugin/metatag/Tag/SchemaRecipeIngredient.php
new file mode 100644
index 0000000..3e227b8
--- /dev/null
+++ b/schema_recipe/src/Plugin/metatag/Tag/SchemaRecipeIngredient.php
@@ -0,0 +1,56 @@
+<?php
+
+namespace Drupal\schema_recipe\Plugin\metatag\Tag;
+
+use \Drupal\schema_metatag\Plugin\metatag\Tag\SchemaItemListBase;
+
+/**
+ * Provides a plugin for the 'schema_recipe_description' meta tag.
+ *
+ * - 'id' should be a globally unique id.
+ * - 'name' should match the Schema.org element name.
+ * - 'group' should match the id of the group that defines the Schema.org type.
+ *
+ * @MetatagTag(
+ * id = "schema_recipe_ingredient",
+ * label = @Translation("ingredient"),
+ * description = @Translation("A ingredient of the item."),
+ * name = "recipeIngredient",
+ * group = "schema_recipe",
+ * weight = 1,
+ * type = "string",
+ * secure = FALSE,
+ * multiple = TRUE
+ * )
+ */
+class SchemaRecipeIngredient extends SchemaItemListBase {
+
+ /**
+ * Generate a form element for this meta tag.
+ */
+ public function form(array $element = []) {
+ $form = parent::form($element);
+ $form['#attributes']['placeholder'] = '[node:ingredient]';
+ return $form;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function output() {
+ $element = parent::output();
+
+ if (!empty($element)) {
+ $values = explode(', ', $this->value());
+
+ foreach( $values AS $v ) {
+ $element['#attributes']['content'][] = trim(preg_replace('/\s+/', ' ', $v));
+ }
+
+
+ }
+
+ return $element;
+
+ }
+}
diff --git a/schema_recipe/src/Plugin/metatag/Tag/SchemaRecipeName.php b/schema_recipe/src/Plugin/metatag/Tag/SchemaRecipeName.php
new file mode 100644
index 0000000..6b0998b
--- /dev/null
+++ b/schema_recipe/src/Plugin/metatag/Tag/SchemaRecipeName.php
@@ -0,0 +1,37 @@
+<?php
+
+namespace Drupal\schema_recipe\Plugin\metatag\Tag;
+
+use \Drupal\schema_metatag\Plugin\metatag\Tag\SchemaNameBase;
+
+/**
+ * Provides a plugin for the 'name' meta tag.
+ *
+ * - 'id' should be a globally unique id.
+ * - 'name' should match the Schema.org element name.
+ * - 'group' should match the id of the group that defines the Schema.org type.
+ *
+ * @MetatagTag(
+ * id = "schema_recipe_name",
+ * label = @Translation("name"),
+ * description = @Translation("Name of the recipe."),
+ * name = "name",
+ * group = "schema_recipe",
+ * weight = 0,
+ * type = "string",
+ * secure = FALSE,
+ * multiple = FALSE
+ * )
+ */
+class SchemaRecipeName extends SchemaNameBase {
+
+ /**
+ * Generate a form element for this meta tag.
+ */
+ public function form(array $element = []) {
+ $form = parent::form($element);
+ $form['#attributes']['placeholder'] = '[node:title]';
+ return $form;
+ }
+
+}
diff --git a/schema_recipe/src/Plugin/metatag/Tag/SchemaRecipePrepTime.php b/schema_recipe/src/Plugin/metatag/Tag/SchemaRecipePrepTime.php
new file mode 100644
index 0000000..3fb77f9
--- /dev/null
+++ b/schema_recipe/src/Plugin/metatag/Tag/SchemaRecipePrepTime.php
@@ -0,0 +1,40 @@
+<?php
+
+namespace Drupal\schema_recipe\Plugin\metatag\Tag;
+
+use \Drupal\schema_metatag\Plugin\metatag\Tag\SchemaDateBase;
+
+/**
+ * Provides a plugin for the 'prepTime' meta tag.
+ *
+ * - 'id' should be a globally unique id.
+ * - 'name' should match the Schema.org element name.
+ * - 'group' should match the id of the group that defines the Schema.org type.
+ *
+ * @MetatagTag(
+ * id = "schema_recipe_prep_time",
+ * label = @Translation("prepTime"),
+ * description = @Translation("Prep time for the recipe."),
+ * name = "prepTime",
+ * group = "schema_recipe",
+ * weight = 4,
+ * type = "string",
+ * secure = FALSE,
+ * multiple = FALSE
+ * )
+ */
+class SchemaRecipePrepTime extends SchemaDateBase {
+
+ /**
+ * Generate a form element for this meta tag.
+ *
+ * We need multiple values, so create a tree of values and
+ * stored the serialized value as a string.
+ */
+ public function form(array $element = []) {
+ $form = parent::form($element);
+ $form['#attributes']['placeholder'] = '[node:recipe_prep_time]';
+ return $form;
+ }
+
+}
diff --git a/schema_recipe/src/Plugin/metatag/Tag/SchemaRecipePublisher.php b/schema_recipe/src/Plugin/metatag/Tag/SchemaRecipePublisher.php
new file mode 100644
index 0000000..607d994
--- /dev/null
+++ b/schema_recipe/src/Plugin/metatag/Tag/SchemaRecipePublisher.php
@@ -0,0 +1,38 @@
+<?php
+
+namespace Drupal\schema_recipe\Plugin\metatag\Tag;
+
+use \Drupal\schema_metatag\Plugin\metatag\Tag\SchemaPersonOrgBase;
+
+/**
+ * Provides a plugin for the 'publisher' meta tag.
+ *
+ * - 'id' should be a globally unique id.
+ * - 'name' should match the Schema.org element name.
+ * - 'group' should match the id of the group that defines the Schema.org type.
+ *
+ * @MetatagTag(
+ * id = "schema_recipe_publisher",
+ * label = @Translation("publisher"),
+ * description = @Translation("Publisher of the recipe."),
+ * name = "publisher",
+ * group = "schema_recipe",
+ * weight = 6,
+ * type = "string",
+ * secure = FALSE,
+ * multiple = FALSE
+ * )
+ */
+class SchemaRecipePublisher extends SchemaPersonOrgBase {
+
+ /**
+ * Generate a form element for this meta tag.
+ */
+ public function form(array $element = []) {
+ $form = parent::form($element);
+ $form['name']['#attributes']['placeholder'] = '[site:name]';
+ $form['url']['#attributes']['placeholder'] = '[site:url]';
+ return $form;
+ }
+
+}
diff --git a/schema_recipe/src/Plugin/metatag/Tag/SchemaRecipeType.php b/schema_recipe/src/Plugin/metatag/Tag/SchemaRecipeType.php
new file mode 100644
index 0000000..ec80928
--- /dev/null
+++ b/schema_recipe/src/Plugin/metatag/Tag/SchemaRecipeType.php
@@ -0,0 +1,46 @@
+<?php
+
+namespace Drupal\schema_recipe\Plugin\metatag\Tag;
+
+use \Drupal\schema_metatag\Plugin\metatag\Tag\SchemaNameBase;
+
+/**
+ * Provides a plugin for the 'schema_recipe_description' meta tag.
+ *
+ * - 'id' should be a globally unique id.
+ * - 'name' should match the Schema.org element name.
+ * - 'group' should match the id of the group that defines the Schema.org type.
+ *
+ * @MetatagTag(
+ * id = "schema_recipe_type",
+ * label = @Translation("@type"),
+ * description = @Translation("The type of recipe."),
+ * name = "@type",
+ * group = "schema_recipe",
+ * weight = -5,
+ * type = "string",
+ * secure = FALSE,
+ * multiple = FALSE
+ * )
+ */
+class SchemaRecipeType extends SchemaNameBase {
+
+ /**
+ * Generate a form element for this meta tag.
+ */
+ public function form(array $element = []) {
+ $form = [
+ '#type' => 'select',
+ '#title' => $this->label(),
+ '#description' => $this->description(),
+ '#empty_option' => t('- None -'),
+ '#empty_value' => '',
+ '#options' => [
+ 'Recipe' => $this->t('Recipe')
+ ],
+ '#default_value' => $this->value(),
+ ];
+ return $form;
+ }
+
+}
diff --git a/schema_recipe/src/Plugin/metatag/Tag/SchemaRecipeUrl.php b/schema_recipe/src/Plugin/metatag/Tag/SchemaRecipeUrl.php
new file mode 100644
index 0000000..f799416
--- /dev/null
+++ b/schema_recipe/src/Plugin/metatag/Tag/SchemaRecipeUrl.php
@@ -0,0 +1,28 @@
+<?php
+
+namespace Drupal\schema_recipe\Plugin\metatag\Tag;
+
+use \Drupal\schema_metatag\Plugin\metatag\Tag\SchemaMainEntityOfPageBase;
+
+/**
+ * Provides a plugin for the 'schema_recipe_main_entity_of_page' meta tag.
+ *
+ * - 'id' should be a globally unique id.
+ * - 'name' should match the Schema.org element name.
+ * - 'group' should match the id of the group that defines the Schema.org type.
+ *
+ * @MetatagTag(
+ * id = "schema_recipe_url",
+ * label = @Translation("url"),
+ * description = @Translation(""),
+ * name = "url",
+ * group = "schema_recipe",
+ * weight = 10,
+ * type = "string",
+ * secure = FALSE,
+ * multiple = FALSE
+ * )
+ */
+class SchemaRecipeUrl extends SchemaMainEntityOfPageBase {
+ // Nothing here yet. Just a placeholder class for a plugin.
+}
diff --git a/src/SchemaMetatagManager.php b/src/SchemaMetatagManager.php
index 77c8449..309ce28 100644
--- a/src/SchemaMetatagManager.php
+++ b/src/SchemaMetatagManager.php
@@ -34,7 +34,9 @@ class SchemaMetatagManager implements SchemaMetatagManagerInterface {
if (empty($items)) {
$items['@context'] = 'http://schema.org';
}
- $items['@graph'][$group_key] = $data;
+ // HACK - only one level
+ $items = array_merge($items, $data);
+ #$items['@graph'][$group_key] = $data;
$group_key++;
}
return $items;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment