Uncategorized

Up and Theming with Drupal 8

Image: Model Thinkshout Employees
Drupal 8 is finally here! We’ve been digging into the code and learning how to install D8 in a way that allow us to sync sites and use it for production work. A lot of things have changed, which we covered in our previous article, Up and Running with Drupal 8. The next step is to see what’s changed in the theming layer, installing a basic theme, and working with the new Twig templating system. There’s a good deal to cover, so let’s jump in!

Creating a Theme##

The steps for setting up a basic site theme are fairly simple: create a custom/THEMENAME directory in web/themes, and then add a THEMENAME.info.yml file with the following:

name: THEMENAME Theme
description: 'D8 theme for THEMENAME site.'
package: Custom
# base theme: classy
type: theme
version: 1.0
core: 8.x
regions:
  header: Header
  content: Content # required!
  sidebar_first: 'Sidebar first'
  footer: Footer

Then you can enable your theme (administer » themes) in the interface. Note that uncommenting base theme: classy will cause you to set Classy as a parent theme. We feel that Classy is great if you want a lot of useful examples, but really clutters up the markup, so use at your own discretion. After rc1, the default theme will be ‘stable,’ and you may want to pull all of the core templates into your theme to ensure you’re working from the latest updated template code.

Also, the theme name must not contain hyphens. So /theme-name/ is invalid (it won’t even show up!), but /theme_name/ is fine.

Now we’ll want to start customizing our theme. Let us say we have a content type called ‘blog’ (machine name: blog), with a field type called ‘Publish Date’ (machine name: field_publish_date).

Despite setting the label of field_publish_date to ‘inline,’ it’s wrapping to a new line due to the fact that it’s a simple, unstyled <div>.

Worse, it has no classes to specifically style it. Let’s set ourselves some goals:

  1. Add the inline styling class(s).
  2. Change the markup for this field, so that we have a class for the label.
  3. Add CSS to style the label, but ONLY for the ‘Blog’ content type.

The documentation for this seemingly simple task is obfuscated and evolving right now, but we were able to get it working correctly using the following steps:

Step 1: Turn on twig debug mode. We also found it helpful at this point to make a copy of web/sites/example.settings.local.php in web/sites/default/ and uncomment the following in settings.php:

if (file_exists(__DIR__ . '/settings.local.php')) {
  include __DIR__ . '/settings.local.php';
}

This will allow you to disable caching during development, which is no longer a simple checkbox in the performance section. Note that disabling caching can be tricky; the drush cr (cache rebuild) command is the most reliable way to ensure the cache is really cleared. You’ll also have to rebuild the cache at least once after turning caching off, so the new cache settings are applied.

Step 2: Make a custom field template.

In this case, the suggested debug fields are:

<!-- FILE NAME SUGGESTIONS:
   * field--node--field-publish-date--blog.html.twig
   * field--node--field-publish-date.html.twig
   * field--node--blog.html.twig
   * field--field-publish-date.html.twig
   * field--datetime.html.twig
   x field.html.twig
-->
<!-- BEGIN OUTPUT from 'core/modules/system/templates/field.html.twig' -->

The highlighted line above shows the template currently being used, suggestions for increased specificity, and the file location (core/modules/system/templates/).

We want to update field_publish_date globally, so we’ll create a template called field--field-publish-date.html.twig

>

Read More 4501
Read More 4502
Read More 4503
Read More 4504
Read More 4505
Read More 4506
Read More 4507
Read More 4508
Read More 4509
Read More 4510
Read More 4511
Read More 4512
Read More 4513
Read More 4514
Read More 4515
Read More 4516
Read More 4517
Read More 4518
Read More 4519
Read More 4520
Read More 4521
Read More 4522
Read More 4523
Read More 4524
Read More 4525
Read More 4526
Read More 4527
Read More 4528
Read More 4529
Read More 4530
Read More 4531
Read More 4532
Read More 4533
Read More 4534
Read More 4535
Read More 4536
Read More 4537
Read More 4538
Read More 4539
Read More 4540
Read More 4541
Read More 4542
Read More 4543
Read More 4544
Read More 4545
Read More 4546
Read More 4547
Read More 4548
Read More 4549
Read More 4550
Read More 4551
Read More 4552
Read More 4553
Read More 4554
Read More 4555
Read More 4556
Read More 4557
Read More 4558
Read More 4559
Read More 4560
Read More 4561
Read More 4562
Read More 4563
Read More 4564
Read More 4565
Read More 4566
Read More 4567
Read More 4568
Read More 4569
Read More 4570
Read More 4571
Read More 4572
Read More 4573
Read More 4574
Read More 4575
Read More 4576
Read More 4577
Read More 4578
Read More 4579
Read More 4580
Read More 4581
Read More 4582
Read More 4583
Read More 4584
Read More 4585
Read More 4586
Read More 4587
Read More 4588
Read More 4589
Read More 4590
Read More 4591
Read More 4592
Read More 4593
Read More 4594
Read More 4595
Read More 4596
Read More 4597
Read More 4598
Read More 4599
Read More 4600
Read More 4601
Read More 4602
Read More 4603
Read More 4604
Read More 4605
Read More 4606
Read More 4607
Read More 4608
Read More 4609
Read More 4610
Read More 4611
Read More 4612
Read More 4613
Read More 4614
Read More 4615
Read More 4616
Read More 4617
Read More 4618
Read More 4619
Read More 4620
Read More 4621
Read More 4622
Read More 4623
Read More 4624
Read More 4625
Read More 4626
Read More 4627
Read More 4628
Read More 4629
Read More 4630
Read More 4631
Read More 4632
Read More 4633
Read More 4634
Read More 4635
Read More 4636
Read More 4637
Read More 4638
Read More 4639
Read More 4640
Read More 4641
Read More 4642
Read More 4643
Read More 4644
Read More 4645
Read More 4646
Read More 4647
Read More 4648
Read More 4649
Read More 4650
Read More 4651
Read More 4652
Read More 4653
Read More 4654
Read More 4655
Read More 4656
Read More 4657
Read More 4658
Read More 4659
Read More 4660
Read More 4661
Read More 4662
Read More 4663
Read More 4664
Read More 4665
Read More 4666
Read More 4667
Read More 4668
Read More 4669
Read More 4670
Read More 4671
Read More 4672
Read More 4673
Read More 4674
Read More 4675
Read More 4676
Read More 4677
Read More 4678
Read More 4679
Read More 4680
Read More 4681
Read More 4682
Read More 4683
Read More 4684
Read More 4685
Read More 4686
Read More 4687
Read More 4688
Read More 4689
Read More 4690
Read More 4691
Read More 4692
Read More 4693
Read More 4694
Read More 4695
Read More 4696
Read More 4697
Read More 4698
Read More 4699
Read More 4700
Read More 4701
Read More 4702
Read More 4703
Read More 4704
Read More 4705
Read More 4706
Read More 4707
Read More 4708
Read More 4709
Read More 4710
Read More 4711
Read More 4712
Read More 4713
Read More 4714
Read More 4715
Read More 4716
Read More 4717
Read More 4718
Read More 4719
Read More 4720
Read More 4721
Read More 4722
Read More 4723
Read More 4724
Read More 4725
Read More 4726
Read More 4727
Read More 4728
Read More 4729
Read More 4730
Read More 4731
Read More 4732
Read More 4733
Read More 4734
Read More 4735
Read More 4736
Read More 4737
Read More 4738
Read More 4739
Read More 4740
Read More 4741
Read More 4742
Read More 4743
Read More 4744
Read More 4745
Read More 4746
Read More 4747
Read More 4748
Read More 4749
Read More 4750
Read More 4751
Read More 4752
Read More 4753
Read More 4754
Read More 4755
Read More 4756
Read More 4757
Read More 4758
Read More 4759
Read More 4760
Read More 4761
Read More 4762
Read More 4763
Read More 4764
Read More 4765
Read More 4766
Read More 4767
Read More 4768
Read More 4769
Read More 4770
Read More 4771
Read More 4772
Read More 4773
Read More 4774
Read More 4775
Read More 4776
Read More 4777
Read More 4778
Read More 4779
Read More 4780
Read More 4781
Read More 4782
Read More 4783
Read More 4784
Read More 4785
Read More 4786
Read More 4787
Read More 4788
Read More 4789
Read More 4790
Read More 4791
Read More 4792
Read More 4793
Read More 4794
Read More 4795
Read More 4796
Read More 4797
Read More 4798
Read More 4799
Read More 4800
Read More 4801
Read More 4802
Read More 4803
Read More 4804
Read More 4805
Read More 4806
Read More 4807
Read More 4808
Read More 4809
Read More 4810
Read More 4811
Read More 4812
Read More 4813
Read More 4814
Read More 4815
Read More 4816
Read More 4817
Read More 4818
Read More 4819
Read More 4820
Read More 4821
Read More 4822
Read More 4823
Read More 4824
Read More 4825
Read More 4826
Read More 4827
Read More 4828
Read More 4829
Read More 4830
Read More 4831
Read More 4832
Read More 4833
Read More 4834
Read More 4835
Read More 4836
Read More 4837
Read More 4838
Read More 4839
Read More 4840
Read More 4841
Read More 4842
Read More 4843
Read More 4844
Read More 4845
Read More 4846
Read More 4847
Read More 4848
Read More 4849
Read More 4850
Read More 4851
Read More 4852
Read More 4853
Read More 4854
Read More 4855
Read More 4856
Read More 4857
Read More 4858
Read More 4859
Read More 4860
Read More 4861
Read More 4862
Read More 4863
Read More 4864
Read More 4865
Read More 4866
Read More 4867
Read More 4868
Read More 4869
Read More 4870

Related Articles

Leave a Reply

Back to top button