Skip to main content
added 44 characters in body
Source Link
No Sssweat
  • 31.6k
  • 14
  • 55
  • 91

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

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.

Source: Drupal 8 - Conditionally hide a form field

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

added 66 characters in body
Source Link
No Sssweat
  • 31.6k
  • 14
  • 55
  • 91

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, FormStateInterface\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.

Source: Drupal 8 - Conditionally hide a form field

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, 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.

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.

Source: Drupal 8 - Conditionally hide a form field

added 66 characters in body
Source Link
No Sssweat
  • 31.6k
  • 14
  • 55
  • 91

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 selectlist (text) field 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, 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_videofield_image with the machine name of your selectimage field.
  • Replace field_imagefield_video with the machine name of your selectvideo field.

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 select field to your content type. Let's say the possible values are "video" and "image".

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, 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_video with the machine name of your select field.
  • Replace field_image with the machine name of your select field.

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, 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.
Source Link
No Sssweat
  • 31.6k
  • 14
  • 55
  • 91
Loading