Skip to content

Commit

Permalink
Issue #3106965 by mtift: Port the npr_story module to Drupal 8
Browse files Browse the repository at this point in the history
  • Loading branch information
mtift authored and mtift committed Jan 17, 2020
1 parent 38615ee commit 6125a5a
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 0 deletions.
9 changes: 9 additions & 0 deletions npr_story/config/install/npr_story.settings.yml
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: ''
8 changes: 8 additions & 0 deletions npr_story/npr_story.info.yml
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
13 changes: 13 additions & 0 deletions npr_story/npr_story.install
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');
}
4 changes: 4 additions & 0 deletions npr_story/npr_story.links.task.yml
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
7 changes: 7 additions & 0 deletions npr_story/npr_story.module
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
*/

7 changes: 7 additions & 0 deletions npr_story/npr_story.routing.yml
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'
130 changes: 130 additions & 0 deletions npr_story/src/Form/NprStoryConfigForm.php
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);
}

}

0 comments on commit 6125a5a

Please sign in to comment.