-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #3106965 by mtift: Port the npr_story module to Drupal 8
- Loading branch information
Showing
7 changed files
with
178 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
drupal_story_content: '' | ||
mappings: | ||
id: '' | ||
subtitle: '' | ||
shortTitle: '' | ||
miniTease: '' | ||
slug: '' | ||
image: '' | ||
audio: '' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
name: 'NPR API Story' | ||
description: 'Configure a content type that can pull from and/or push to the NPR API' | ||
core: 8.x | ||
package: NPR | ||
type: module | ||
configure: npr_story.config_form | ||
dependencies: | ||
- npr_api |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Install, update, and uninstall functions for the npr_story module. | ||
*/ | ||
|
||
/** | ||
* Implements hook_uninstall(). | ||
*/ | ||
function npr_story_uninstall() { | ||
\Drupal::configFactory()->reset('npr_story.settings'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
npr_story.config_form: | ||
route_name: npr_story.config_form | ||
title: 'Story Settings' | ||
base_route: npr_api.config_form |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Configure a content type that can pull from and/or push to the NPR API | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
npr_story.config_form: | ||
path: /admin/config/services/npr/story_config | ||
defaults: | ||
_title: 'Story Settings' | ||
_form: \Drupal\npr_story\Form\NprStoryConfigForm | ||
requirements: | ||
_permission: 'administer npr api' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Contains \Drupal\npr_story\Form\NprStoryConfigForm. | ||
*/ | ||
|
||
namespace Drupal\npr_story\Form; | ||
|
||
use Drupal\Core\Entity\EntityFieldManagerInterface; | ||
use Drupal\Core\Entity\EntityTypeManagerInterface; | ||
use Drupal\Core\Form\ConfigFormBase; | ||
use Drupal\Core\Form\FormStateInterface; | ||
use Drupal\Core\Render\Element; | ||
use Drupal\npr_story\NprClient; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
|
||
class NprStoryConfigForm extends ConfigFormBase { | ||
|
||
/** | ||
* The entity type manager. | ||
* | ||
* @var \Drupal\Core\Entity\EntityTypeManagerInterface | ||
*/ | ||
protected $entityTypeManager; | ||
|
||
/** | ||
* The entity field manager. | ||
* | ||
* @var \Drupal\Core\Entity\EntityFieldManagerInterface | ||
*/ | ||
protected $entityFieldManager; | ||
|
||
/** | ||
* Constructs a new NprStoryConfigForm. | ||
* | ||
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager | ||
* The entity type manager. | ||
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_field_manager | ||
* The entity field manager. | ||
*/ | ||
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager) { | ||
$this->entityTypeManager = $entity_type_manager; | ||
$this->entityFieldManager = $entity_field_manager; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function create(ContainerInterface $container) { | ||
return new static( | ||
$container->get('entity_type.manager'), | ||
$container->get('entity_field.manager') | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getFormId() { | ||
return 'npr_story_config_form'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getEditableConfigNames() { | ||
return ['npr_story.settings']; | ||
} | ||
|
||
public function buildForm(array $form, \Drupal\Core\Form\FormStateInterface $form_state) { | ||
|
||
$config = $this->config('npr_story.settings'); | ||
|
||
$drupal_content_types = array_keys($this->entityTypeManager->getStorage('node_type')->loadMultiple()); | ||
$content_type_options = array_combine($drupal_content_types, $drupal_content_types); | ||
$form['drupal_story_content'] = [ | ||
'#type' => 'select', | ||
'#title' => $this->t('Drupal Story content type'), | ||
'#default_value' => $config->get('drupal_story_content'), | ||
'#options' => $content_type_options, | ||
]; | ||
|
||
// Field Mappings. | ||
$form['field_mappings'] = [ | ||
'#type' => 'details', | ||
'#title' => $this->t('Story field mappings'), | ||
'#open' => TRUE, | ||
]; | ||
$story_content_type = $config->get('drupal_story_content'); | ||
if (!empty($story_content_type)) { | ||
$story_fields = array_keys($this | ||
->entityFieldManager | ||
->getFieldDefinitions('node', $story_content_type)); | ||
$story_field_options = ['unused' => 'unused'] + array_combine($story_fields, $story_fields); | ||
$npr_story_fields = $config->get('mappings'); | ||
foreach ($npr_story_fields as $field_name => $field_value) { | ||
$default = !empty($npr_story_fields[$field_name]) ?? 'unused'; | ||
$form['field_mappings'][$field_name] = [ | ||
'#type' => 'select', | ||
'#title' => $field_name, | ||
'#options' => $story_field_options, | ||
'#default_value' => $npr_story_fields[$field_name], | ||
]; | ||
} | ||
} | ||
|
||
return parent::buildForm($form, $form_state); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function submitForm(array &$form, FormStateInterface $form_state) { | ||
$values = $form_state->getValues(); | ||
$config = $this->config('npr_story.settings'); | ||
|
||
$config->set('drupal_story_content', $values['drupal_story_content']); | ||
|
||
$npr_story_fields = $config->get('mappings'); | ||
foreach ($npr_story_fields as $field_name => $field_value) { | ||
$config->set('mappings.' . $field_name, $values[$field_name]); | ||
} | ||
$config->save(); | ||
|
||
parent::submitForm($form, $form_state); | ||
} | ||
|
||
} | ||
|