Skip to content

Instantly share code, notes, and snippets.

@cweagans
Created November 3, 2015 15:56
Show Gist options
  • Save cweagans/54a3759377e004858ea9 to your computer and use it in GitHub Desktop.
Save cweagans/54a3759377e004858ea9 to your computer and use it in GitHub Desktop.
diff --git a/docroot/modules/custom/pc_editorial/pc_editorial.module b/docroot/modules/custom/pc_editorial/pc_editorial.module
index 30de354..0edaeb0 100644
--- a/docroot/modules/custom/pc_editorial/pc_editorial.module
+++ b/docroot/modules/custom/pc_editorial/pc_editorial.module
@@ -1,5 +1,7 @@
<?php
+use Drupal\Core\Form\FormStateInterface;
+
function pc_editorial_entity_type_alter(array &$entity_types) {
/** @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
// Set the controller class for nodes to an alternate implementation of the
@@ -9,3 +11,13 @@ function pc_editorial_entity_type_alter(array &$entity_types) {
$entity_types['node']->setFormClass('edit', 'Drupal\pc_editorial\Form\PcEditorialNodeForm');
}
}
+
+/**
+ * Implements hook_form_alter().
+ */
+function pc_editorial_form_alter(&$form, FormStateInterface $form_state, $form_id) {
+ if (($form_id == 'media_image_form') || ($form_id == 'media_image_edit_form')) {
+ $node_form = Drupal\pc_editorial\Form\PcEditorialNodeForm::create(\Drupal::getContainer());
+ $node_form->addCancelButton($form, $form_state);
+ }
+}
diff --git a/docroot/modules/custom/pc_editorial/src/Form/PcEditorialNodeForm.php b/docroot/modules/custom/pc_editorial/src/Form/PcEditorialNodeForm.php
index 2fdce6e..62c103a 100644
--- a/docroot/modules/custom/pc_editorial/src/Form/PcEditorialNodeForm.php
+++ b/docroot/modules/custom/pc_editorial/src/Form/PcEditorialNodeForm.php
@@ -45,17 +45,21 @@ public function actions(array $form, FormStateInterface $form_state) {
$element['publish']['#value'] = t('Publish');
$element['unpublish']['#value'] = t('Save as draft');
- $element['#attached']['library'][] = 'pc_editorial/cancel';
- $element['#attached']['drupalSettings']['pc_editorial']['cancel']['question'] = $this->config_factory->get('pc_editorial.configurations')->get('question');
- $element['actions']['cancel'] = array(
- '#type' => 'link',
- '#title' => $this->t('Cancel'),
- '#url' => Url::fromRoute($this->config_factory->get('pc_editorial.configurations')->get('cancel_path')),
- '#attributes' => array(
- 'class' => array('button', 'pc-editorial-cancel'),
- )
- );
+ $this->addCancelButton($form, $form_state);
return $element;
}
+
+ public function addCancelButton(array &$form, FormStateInterface $form_state) {
+ $form['#attached']['library'][] = 'pc_editorial/cancel';
+ $form['#attached']['drupalSettings']['pc_editorial']['cancel']['question'] = $this->config_factory->get('pc_editorial.configurations')->get('question');
+ $form['actions']['cancel'] = array(
+ '#type' => 'link',
+ '#title' => $this->t('Cancel'),
+ '#url' => Url::fromRoute($this->config_factory->get('pc_editorial.configurations')->get('cancel_path')),
+ '#attributes' => array(
+ 'class' => array('button', 'pc-editorial-cancel'),
+ )
+ );
+ }
}
diff --git a/docroot/modules/custom/pc_image/pc_image.module b/docroot/modules/custom/pc_image/pc_image.module
index 960172a..986e0c9 100644
--- a/docroot/modules/custom/pc_image/pc_image.module
+++ b/docroot/modules/custom/pc_image/pc_image.module
@@ -18,25 +18,4 @@ function pc_image_entity_base_field_info_alter(&$fields, EntityTypeInterface $en
}
}
-/**
- * Implements hook_form_alter().
- */
-function pc_image_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
- if (($form_id == 'media_image_form') || ($form_id == 'media_image_edit_form')) {
- if (isset($form['actions']['delete'])) {
- $form['actions']['delete']['#title'] = t('Cancel');
- $form['actions']['delete']['#url'] = Url::fromRoute('system.admin_content');
- $form['actions']['delete']['#attributes']['class'] = array('button', 'pc-editorial-cancel');
- }
- else{
- $form['actions']['cancel'] = array(
- '#type' => 'link',
- '#title' => t('Cancel'),
- '#weight' => 20,
- '#url' => Url::fromRoute('system.admin_content'),
- '#attributes' => array('class' => array('button', 'pc-editorial-cancel')),
- );
- }
- }
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment