It used to be the same for foreign names. I'd want to register on a Spanish webpage. I was asked for both my surnames. As I don't have two surnames, and as it was often a required field, I'd try with an X or a dash. Sometimes that didn't work; I'm still Chris Thompson Nil on a fair few databases. It's a problem that has almost gone away nowadays. There's still space for two surnames but only the first will be compulsory. This is because, as I'm sure you know, most Spaniards have two surnames; one from the mother and the other from the father. The usual order is father's name first, mother's second. So Juan Ortega Perez and Sara Blanes Hidalgo have sex that results in baby Nerea. Traditionally Nerea would be Nerea Ortega Blanes. In everyday circumstances she would be Nerea Ortega. If she wrote a book it would be filed, surname wise, in the Os. However, because her mum is feisty and because her dad wants to be a modern man, they may choose to call her Nerea Blanes Ortega; the law changed to allow that in 2017. When Nerea gets to 18, if she doesn't like the order her parents chose, she has the right to change her surnames around. There are lots of variations on this basic idea but let's keep it simple.
I'd always presumed this double surname thing went back hundreds of years but it turns out that the codification of double surnames is relatively recent. In fact the choice of surnames in Spain has traditionally been a complete free for all. There was a general understanding that the surname couldn't be malicious, so naming your child, toilet brush, for instance, wasn't on. Malicious surnames aside the system was completely random. Usually, the first born adopted the name of the father and the rest of the brothers or sisters adopted other family surnames. This would mean that siblings might have a variety of surnames. There was a tendency for boys to take the surname of their father and for girls to take a surname from the mother or another woman in the family. As you may imagine this could cause a great deal of confusion for anyone trying to keep tabs on the population.
In the distant past Spain was made up of lots of different kingdoms. One of the biggest was Castile. By the 16th Century the rich Castilians had started to use the double surname method. My guess is that they did it mainly for matters of inheritance and property rights. That system was also adopted by the Basques about the same time. It made administration simpler. By 1833 this system of two names was very common so that the first Spanish census, in 1857, had boxes for the paternal and maternal surnames. When the civil registry was established in 1871 they continued the two box custom but it wasn't till 1889, when the first Spanish Civil Code (A civil code is a set of rules and laws about how individuals relate to private and public bodies) was established, that it became compulsory for children to carry the surnames of both parents. This same system of double surnames was largely adopted in Latin America too because of their close links to Spain. Immigrants to Spain who take Spanish nationality are required to register two surnames
As an example of this change in the pattern of surnames I thought I'd use some old examples of famous Spanish names who only had one surname because they were named before the double barreling became the norm. I thought of Cervantes, the author, of Hernán Cortés and of Francisco Pizarro, conquistadores both. I realised that they were all men but I couldn't think of a single historical Spanish woman from the 15th or 16th or 17th Centuries who wasn't either a royal or a religious personality - such is the power of patriarchy. So, men as examples but, when I checked their names, it turned out that their names were not constructed, as I expected, following the John Smith or Akosua Busia model. The author of Don Quixote (El ingenioso hidalgo don Quijote de la Mancha if you prefer) was Miguel de Cervantes Saavedra. The Mexico man was Hernán Cortés de Monroy y Pizarro Altamirano and the bloke who conquered, what is now largely, Peru was Francisco Pizarro González. None of the simple given name plus family name for them.
So double barrelled may now be the norm but how did the surnames first arise? Bear in mind the thing above about families using several different surnames and the thing below about forming surnames - so the fat one got called Fatty, the healer got called Healer and the one from the valley got called From the Valley etc. Indeed lots of surnames were simply made up and came from nicknames or descriptive features: Rubio, blonde, Calvo, bald and Delgado, thin, for instance.
Surnames that are associated with trades and jobs are common in Spain as they are in English, The Coopers, Archers and Fletchers of the UK become the Zapateros, Herreros, and Pastores - shoemakers, smiths and shepherds of Spain. I always think it's a good thing that surnames are now well established or it we'd all be stuck with things like "Hello Ms. User Experience Designer" or "How do you do Mr. Sustainability Manager?"
Lots of Spanish surnames are patronymic, that is a name derived from the name of a father or ancestor. These sort of names are common all over the world, for instance the Icelanders with surnames formed by adding -son to the father's name for sons - Magnus Magnusson or -dottir for daughters as in Björk Guðmundsdóttir. In Spanish these are the names that end in ez, oz or iz - Muñoz, Sánchez, Martínez or Albéniz. Sancho's lad, Martin's son etc. We Britons do it too, Thompson, Robertson, Davidson.
Then there are the toponymic surnames, that is to say, names that come from a town or other place. Some of those are simply place names - Peñaranda, Catalán, Sevillano and the like exactly as we Britons do with Sarah Lancashire or Jodie Bedford. Then there are lots which use de or del or de la which means of the or from the. So María Teresa Fernández de la Vega, of the water meadow, Antonio Cánovas del Castillo, of the castle, Vicente del Bosque, of the wood and Luis de la Fuente, from the fountain.
So, the next time your middle name gets used as your first surname, if you didn't know, well, now you do.