On Github wizonesolutions / rewriting-modules-d8
Kevin Kaland - wizonesolutions
Presentation source: https://github.com/wizonesolutions/rewriting-modules-d8
Everything.
Yes, I'll tell you more.
<insert learning curve image with people falling off that @mortendk uses all the time here>
name: 'FillPDF' type: module description: 'Allows users to populate PDF forms from website data.' core: 8.x package: Other version: VERSION dependencies: - file - link - token - views
# Form example fillpdf.settings: path: '/admin/config/media/fillpdf' defaults: _form: '\Drupal\fillpdf\Form\FillPdfSettingsForm' _title: 'FillPDF settings' requirements: _permission: 'administer pdfs' options: _admin_route: TRUE
entity.fillpdf_form.edit_form: path: '/admin/structure/fillpdf/{fillpdf_form}' defaults: _entity_form: fillpdf_form.edit _title: 'Edit FillPDF form' requirements: _permission: 'administer pdfs' # todo: do we have an administer own pdfs perm? options: _admin_route: TRUE
fillpdf.populate_pdf: path: '/fillpdf' defaults: _controller: '\Drupal\fillpdf\Controller\HandlePdfController::populatePdf' requirements: _custom_access: '\Drupal\fillpdf\FillPdfAccessController::checkLink'
Pretty much the same
buildForm() method for non-entity forms
form() for entity forms
Return a good ol' render array
Validation, submission similar
$form_state is an object now!!!
// In a ConfigFormBase form $config = $this->config('fillpdf.settings'); $fillpdf_service = $config->get('fillpdf_service_backend');
// Save form values. $this->config('fillpdf.settings') ->set('fillpdf_service_backend', $form_state->getValue('fillpdf_service_backend')) ->set('fillpdf_api_key', $form_state->getValue('fillpdf_api_key')) ->set('fillpdf_remote_protocol', $form_state->getValue('fillpdf_remote_protocol')) ->set('fillpdf_pdftk_path', $form_state->getValue('fillpdf_pdftk_path')) ->save();
See fago's awesome slide deck.
It's good.
tl;dr: ContentEntityType and baseFieldDefinitions()
(or ConfigEntityType)
fillpdf.admin.settings: version: VERSION js: fillpdf.js: {} css: component: fillpdf.css: {} dependencies: - core/jquery - core/drupal
$form['#attached'] = array( 'library' => array('fillpdf/fillpdf.admin.settings') );
\Drupal::config('foo.bar')->get('baz'), but you should use the injected versions except in procedural code (which should be rare)
$this->config->get('foo.bar')->get('baz')
So what's going on in this example?
class HandlePdfController extends ControllerBase { protected $linkManipulator; public function __construct(FillPdfLinkManipulatorInterface $link_manipulator) { $this->linkManipulator = $link_manipulator; } // ControllerBase implements \Drupal\Core\DependencyInjection\ContainerInjectionInterface, // which provides the create method and makes this work. public static function create(ContainerInterface $container) { // factory method; returns instance of *this* class return new static($container->get('fillpdf.link_manipulator')); } }
This is also the pattern to inject classes you need when the class you're extending does not already.
services: plugin.manager.fillpdf_backend: class: Drupal\fillpdf\FillPdfBackendManager parent: default_plugin_manager fillpdf.link_manipulator: class: Drupal\fillpdf\Service\FillPdfLinkManipulator fillpdf.admin_form_helper: class: Drupal\fillpdf\Service\FillpdfAdminFormHelper arguments: ['@module_handler']
class FillPdfFormViewsData extends EntityViewsData { /** * {@inheritdoc} */ public function getViewsData() { $data = parent::getViewsData(); $data['fillpdf_forms']['table']['group'] = $data['fillpdf_forms']['table']['base']['title'] = t('FillPDF forms'); $data['fillpdf_forms']['table']['base']['help'] = t('FillPDF forms are uploaded on the FillPDF administration page and are used by the FillPDF module.'); return $data; } }
Twitter: @wizonesolutions
E-mail: Drupal.org contact form
Website/blog: WizOne Solutions
Continue to next slide for useful links!
Veuillez faîtes semblance que cette ligne contient une bonne blague pour que nous puissons tromper les étrangers.