1

On a basic page I have two similar fields. One for an image and the other for a video.

Either can be populated, but preferably not both, as I don't not want two fields to be displayed on the front end. I also want consistency across multiple view modes with only one being displayed in teasers etc

This trouble is, some editors do not appreciate the reasons and populate both fields, so I need to method for limiting this.

With D7 I found a good solution by using Conditional Fields module. By using a select list (for the file type). the corresponding field input would be exposed in the admin and the same field would be displayed on the frontend. Even if both fields were populated, only the current selection would display.

The D8 version of conditional_fields does not yet have this functionality, and may not get it. See my issue

I am therefore looking for an alternative method and hoped one of the experts here might some suggestions. I am looking for something simple and could not consider anything unsuitable for an untrained editor.

4
  • You could use one field for both and restrict the field to one value. For example a media field which allows both media types Image+Video. The same is possible with a paragraph field for which you create two paragraph types for an image and a video.
    – 4uk4
    Commented Jun 20, 2018 at 11:32
  • Another option is to use hook_form_alter and make it conditional. Someone made a blog entry about this Drupal 8 - Conditionally hide a form field
    – No Sssweat
    Commented Jun 20, 2018 at 11:35
  • @4k4 Thank you for these suggestions. I have tried both and learned new stuff in doing so, but I don't think either method will allow me a trouble free upgrade route for D7 sites. Commented Jun 20, 2018 at 14:26
  • @NoSssweat This looks more more suitable if I can get it working. A bit confusing because it is rare for me to attempt any coding. Commented Jun 20, 2018 at 14:29

1 Answer 1

0

A bit confusing because it is rare for me to attempt any coding.

For this example I will pretend the content type is "Article".

First go to manage fields, and add a new field of list (text) to your content type. Let's say the possible values are "video" and "image". So For allowed values list, put

image|image
video|video

In /themes/your_theme_name/your_theme_name.theme add the following

/**
 * Implements hook_form_FORM_ID_alter().
 */
function your_theme_name_form_node_article_edit_form_alter(&$form, \Drupal\Core\Form\FormStateInterface &$form_state, $form_id) {
  $form['field_image']['#states'] = [
    'visible' => [
      'select[name="field_select"]' => ['value' => 'image']
    ]
  ];
  $form['field_video']['#states'] = [
     'visible' => [
       'select[name="field_select"]' => ['value' => 'video']
    ]
  ];
}
  • Replace your_theme_name with your actual theme name.
  • Replace article with your actual content type name.
  • Replace field_select with the machine name of your select field.
  • Replace field_image with the machine name of your image field.
  • Replace field_video with the machine name of your video field.

Lastly, clear and flush all cache to see changes.

Source: Drupal 8 - Conditionally hide a form field

11
  • This is really helpful, thank you. I haven't yet done a form_alter with D8 (only did one with D7 :-). Should this code go in a custom module or in themename.theme ? Commented Jun 21, 2018 at 9:35
  • Sorry I should have read this more carefully before posting. Though I am confused about whether this will effect the needed changes in both the node edit form and the main theme. Guess I need to try and see what happens. Commented Jun 21, 2018 at 9:44
  • @MrPaulDriver you can do it in a custom module as well if you like. Here, as long as the main theme remains enabled, it will trigger the form alter regardless of whatever you use as an admin theme, which is the theme you see in node edit.
    – No Sssweat
    Commented Jun 21, 2018 at 9:48
  • Not entirely sure what I am doing here, but I have found that putting code in my themename.theme does not appear to work. Putting it in admin.theme does though - kind of. Except one field is always viable regardless of the selection. I am also seeing an error which appears related to a core bug connected with states.drupal.org/project/drupal/issues/2700667 I've put my code and the error in a pastebin. pastebin.com/jDBDSTkq and pastebin.com/atvn593k Commented Jun 21, 2018 at 12:51
  • @MrPaulDriver you're probably doing something wrong in your theme. You're never suppose to touch any Drupal core file, that includes the Seven theme, because when you update Drupal 8 you will lose all of your seven.theme changes as this file will get overwritten by the default core seven.theme
    – No Sssweat
    Commented Jun 21, 2018 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.