Exercise 3.2: Add and configure a media field

In this exercise you will add an Image media field to a content type and learn how to configure it. There are four steps involved:
  1. 1.
    Create an image style
  2. 2.
    Configure an Image Media entity view mode to use an Image style
  3. 3.
    Check permissions
  4. 4.
    Add image field to content type
Image styles are set and configured in the Image styles admin page.

1. Create an image style

  1. 1.
    Go to ConfigurationMediaImage styles.
  2. 2.
    To create our new image style, click on the Add image style button at the top of the page.
  3. 3.
    Name it Job Posting Image (given the image style will be applied to the “Job posting” Featured Image field.)
  4. 4.
    Click the Create new style button to save the new image style. You’ll then be taken to the Edit style Job Posting Image page as seen below.
  5. 5.
    The next step is to add the Scale effect. Under Effect select Scale from the dropdown menu.
  6. 6.
    Click Add. You’ll then be taken to the Add scale effect page.
  7. 7.
    Enter the following settings:
    • Width: 1200
    • Height: leave empty This allows you to have a flexible image height and keep the original proportions of the image.
    • Leave the Allow Upscaling checkbox unselected Image upscaling usually leads to poor image quality.
    • Click Add effect.
  8. 8.
    Your image style has been saved. You can make additional changes and then click the Save button to save those changes.

2. Configure an Image Media entity view mode to use an Image style

  1. 1.
    Navigate to the StructureMedia typesImageManage display (/admin/structure/media/manage/image/display). You should see some out-of-the-box available view modes.
  2. 2.
    Scroll to the bottom of the page and expand the Custom display settings pane. You can see a few View modes preselected by default. Each selected checkbox here represents an enabled View mode at the top of the page.
    Since we need to apply our custom Image style, we have to create a new View mode so that we don’t affect the display of existing images that are configured to display with other View modes.
  3. 3.
    Click on the Manage view modes link (see the previous screenshot) in a new tab/browser window.
  4. 4.
    Click the Add view mode button.
  5. 5.
    Select Media from the list.
  6. 6.
    Give it a name “Job Posting featured” and click the Save button.
  7. 7.
    Return to the previous browser tab/window - go back to step 2b above and refresh the page to get the updated list of View modes.
  8. 8.
    Repeat step 2b above by scrolling down and expanding the Custom display settings pane. You should now see the Job Posting featured view mode as a checkbox option. Select it and save the page.
  9. 9.
    Note, that the Job Posting featured tab is now available at the top. Click it to navigate to the view mode configuration page.
  10. 10.
    At the moment, the Image field is the only one available for display and it is set to show as the originally uploaded image, without any image style. We need to change it to apply our custom Image style.
  11. 11.
    Click the gear link on the right to change from the Original image to the image style Job Posting Image.. Press the Update button to save the changes.
  12. 12.
    Confirm the image style option has changed:
  13. 13.
    Press the Save button to save the changes.

3. Check permissions

  1. 1.
    Go to PeoplePermissions and double check the permissions for the content type: Job posting.
  2. 2.
    Make sure the Content Author role has permissions to Create, Edit any and Delete any Job posting.
  3. 3.
    Click Save permissions

4. Add image field to content type

Go to StructureContent typesJob PostingManage fields page. Add a field following these steps:
  1. 1.
    Click the Add field button.
  2. 2.
    In the “Reuse an existing field” selector, choose Entity reference: field_featured_image.
  3. 3.
    Ensure the label reads “Featured Image”.
  4. 4.
    Click the Save and continue button.
  5. 5.
    In the next page, select the Create referenced entities if they don't already exist toggle in the Reference type fieldset.
  6. 6.
    Select Image as Media type.
  7. 7.
    Leave the remaining values to their defaults and Save settings. This will take you back to the Manage fields page.
  8. 8.
    Navigate to the Manage form display (secondary tabs).
  9. 9.
    Locate the Featured image field, set the Widget to Entity Browser.
  10. 10.
    Click on the cogs icon to configure the Entity Browser.
  11. 11.
    Change the Widget for the field as displayed below, then click on Update.