How the meaning is derived for 得意忘形?
It is always good practice to look up the etymology of an idiom whenever you are learning one:

（阮籍）嗜酒能嘯，善彈琴。當其得意，忽忘形骸。《晉書．卷四九．阮籍列傳》(教育部成語字典)
Ruan Ji enjoys wine and a good wail; he is also skilled in playing the guqin. Whenever he derives joy (from these), without warning, he forgets his physical form.

Ruan Ji was one of the Seven Sages of the Bamboo Grove (竹林七賢). They developed a particularly hedonistic lifestyle – it is important to understand the original (positive) meaning of the idiom with this context.
While in modern Chinese, we see 得意 as an indivisible word that means 'to be proud of oneself; to be complacent', it is separable in classical Chinese (and has a different meaning too), which is arguably more accurate when we are interpreting idioms with a literal approach.
得 is a verb meaning 'to get; to acquire'.
意 is obscurely polysemous. It can mean 'idea' (e.g., 意義, 意在言外 in modern Chinese) and 'intention' (e.g., 意圖, 不懷好意). But here, it should mean 'interest; pleasure'. Consider another similar example:

醉翁之意不在酒，在乎山水之間也。《醉翁亭記》
This joy from the mountains and the water he feels within his mind; he merely ascribes it to the wine.

This suggests, in very crude terms, that the enjoyment of a drinker is not derived from the wine itself, but the environment in which the wine is drunk. In modern Chinese, we say someone is having an ulterior motive if we say 醉翁之意不在酒.

However, idioms are known to be especially prone to semantic shift. 得意忘形 is no longer used to describe someone as being hedonistic (which is neutral, perhaps somewhat approving), but careless due to extreme glee (which is definitely disapproving).


References
Lian, Xianda. “The Old Drunkard Who Finds Joy in His Own Joy -Elitist Ideas in Ouyang Xiu's Informal Writings.” Chinese Literature: Essays, Articles, Reviews (CLEAR), vol. 23, 2001, pp. 1–29. JSTOR, www.jstor.org/stable/495498. Accessed 20 Apr. 2021.
Hegel, Robert E. &quot;A Record of the Pavilion of an Intoxicated Old Man,&quot; in The Columbia Anthology of Traditional Chinese Literature, ed. Victor H. Mair (New York: Columbia University Press, 1994), pp. 590–591.